POST without redirection with PHP

I have a simple form for the mailing list that I found at http://www.notonebit.com/projects/mailing-list/

The problem is, when I click on the “Submit All I Want” button, it displays a message in the current form that says “Thank you for subscribing” without any forwarding. Instead, he directs me to a whole new page.

<form method="POST" action="mlml/process.php"> 
    <input type="text" name="address" id="email" maxlength="30" size="23"> 
    <input type="submit" value="" id="submit"name="submit"  >
</form>     
+5
source share
8 answers

AJAX . , , . Javascript. , ( ), - Javascript.

AJAX JQuery.

, URL-. JQuery HTTP_X_REQUESTED_WITH, script. URL-, ( , AJAX ). , URL- , Javascript. script , , , Ajax.

+4

jQuery:

<form id="mail_subscribe"> 
  <input type="text" name="address" id="email" maxlength="30" size="23">
  <input type="hidden" name="action" value="subscribe" />
  <input type="submit" value="" id="submit"name="submit"  >
</form>

<p style="display: none;" id="notification">Thank You!</p>

<script>
$('#mail_subscribe').submit(function() {
  var post_data = $('#mail_subscribe').serialize();
  $.post('mlml/process.php', post_data, function(data) {
    $('#notification').show();
  });
});

</script>

process.php:

<?php

if(isset($_POST['action'])) {

switch($_POST['action']) {
  case 'subscribe' :
  $email_address = $_POST['address'];

  //do some db stuff...
  //if you echo out something, it will be available in the data-argument of the
  //ajax-post-callback-function and can be displayed on the html-site
  break;
}

}

?>
+3

- action.

:

    <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>"> 
        <input type="text" name="address" id="email" maxlength="30" size="23" /> 
        <input type="submit" value="" id="submit" name="submit" />
    </form> 

<?php if (isset($_POST['submit'])) : ?>
   <p>Thank you for subscribing!</p>
<?php endif; ?>

"", .

, , , , script . , .

+2

AJAX. , JavaScript Brwoser. -, .

+1

, , , , :

"- php target" , . , php , http_post_data ( POST ). , .

0

php, , .

<form method="post" action="http://yoururl.com/recv.php" target="_self"> <input type="text" name="somedata" id="somedata" /> <input type="submit" name="submit" value="Submit!" /> </form>

php- -, ,

header( 'Location: http://yourotherurl.com/formpage' );

, ,

$success = "true"; header( 'Location: http://yourotherurl.com/formpage?success='.$success);

formpage

$success = $_GET ['success'];

if ($ success == "true") {echo 'Your success message'; } else {echo 'Your error message';

0
source

ENGLISH Version

Nobody seems to have solved this problem without javascript or ajax

You can also do the following.

Save the php file with functions and then submit them to your page index

Example

index.php

<div>
<?php include 'tools/edit.php';?>
<form method="post">
<input type="submit" name="disable" value="Disable" />
<input type="submit" name="enable" value="Enable" />
</form>
</div>

Tools.php (this can be any name, note that it is stored in the lame tools folder)

<?php
if(isset($_POST['enable'])) { 
echo "Enable";
} else {
}

if(isset($_POST['enable'])) { 
echo "Enable";
} else {
}
?>
0
source

Use



form onsubmit="takeActions();return false;"

function takeAction(){ var value1 = document.getElementById('name').innerHTML; // make an AJAX call and send all the values to it // Once , you are done with AJAX, time to say Thanks :) document.getElementById('reqDiv').innerHTML = "Thank You for subscribing"; }

-2
source

All Articles