POST without redirect with PHP
Asked Answered
F

10

9

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

The problem is when I click submit all I want it to do is display a message under the current form saying "Thanks for subscribing" without any redirect. Instead, it directs me to a completely 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>     
Fedora answered 24/7, 2011 at 9:29 Comment(0)
A
6

You will need AJAX to post the data to your server. The best solution is to implement the regular posting, so that will at least work. Then, you can hook into that using Javascript. That way, posting will work (with a refresh) when someone doesn't have Javascript.

If found a good article on posting forms with AJAX using JQuery .

In addition, you can choose to post the data to the same url. The JQuery library will add the HTTP_X_REQUESTED_WITH header, of which you can check the value in your server side script. That will allow you to post to the same url but return a different value (entire page, or just a specific response, depending on being an AJAX request or not). So you can actually get the url from your form and won't need to code it in your Javascript too. That allows you to write a more maintanable script, and may even lead to a generic form handling method that you can reuse for all forms you want to post using Ajax.

Alaster answered 24/7, 2011 at 9:51 Comment(0)
S
5

Quite simple with 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>

and in your 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;
}

}

?>
Succotash answered 24/7, 2011 at 10:14 Comment(0)
D
3

It redirects to a different page because of your action attribute.

Try:

    <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; ?>

The page will show your "Thank You" message after the user clicks your submit button.

Also, since I don't know the name of the page your code is on, I inserted a superglobal variable that will insert the the filename of the currently executing script, relative to the document root. So, this page will submit to itself.

Durnan answered 24/7, 2011 at 9:32 Comment(3)
Although, if you are intending the page to not be refreshed, then GameScripting's answer is correct. You will required AJAX if you are processing information in PHP. Otherwise, you can just use Javascript as Prashant Singh suggests.Durnan
Also, you can just leave action empty if you want to post to the current url. No need to involve PHP there.Alaster
@Alaster Thanks for that. I never knew or have tested how the form behaves without the action attribute.Durnan
P
2

to send a form request without redirecting is impossible in php but there is a way you can work around it.

<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>

then for the php page its sending to have it do something but DO NOT echo back a result, instead simply redirect using

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

if you want it to send back a success message simply do

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

and on the formpage add

$success = $_GET['success'];

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

Pogonia answered 29/8, 2016 at 11:24 Comment(0)
P
2

Return and print the contents of another page on the current page.

index.php

<html>
<body>
<p>index.php</p>  
<form name="form1" method="post" action="">
  Name: <input type="text" name="search">
  <input type="submit">
</form>
 
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $_POST['search'];
  include 'test.php';
}
?>
    
</body>
</html> 

test.php

<?php
   echo 'test.php <br/>';
   echo 'data posted is: ' . $_POST['search'];
?>

Result:

Result:

Primaveria answered 21/9, 2021 at 12:41 Comment(0)
M
1

You have to use AJAX. But that requires JavaScript to be active at the users Brwoser. In my opinion it's the only way to do without redirect.

Matildematin answered 24/7, 2011 at 9:31 Comment(0)
W
0

Just an idea that might work for you assuming you have no control over the page you are posting to:

Create your own "proxy php target" for action and then reply with the message you want. The data that was posted to your php file can then be forwarded with http_post_data (Perform POST request with pre-encoded data). You might need to parse it a bit.

We answered 24/7, 2011 at 9:55 Comment(0)
B
0

ENGLISH Version

It seems that no one has solved this problem without javascript or ajax

You can also do the following.

Save a php file with the functions and then send them to the index of your page

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 (It can be any name, note that it is kept in a folder lame tools)

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

if(isset($_POST['disable'])) { 
echo "Disable";
} else {
}
?>
Beery answered 8/6, 2017 at 22:16 Comment(0)
F
0

I'm surprised nobody has mentioned this, as it has been 12 and a half years, but you can just do this:

index.php

<form method="post" action="<?php include 'form-action.php';?>" enctype="multipart/form-data">
      <input type="text" name="input1" placeholder="Enter text...">
      <input type="text" name="input2" placeholder="Enter more text...">
      <input type="submit" value="submit" name="submit">
</form>

form-action.php

<?php
    $input1 = $_POST['input1'];
    $input2 = $_POST['input2'];

    //Do stuff with your inputs

?>

You can put the PHP code directly into the action, without the include, but the include just makes the code much easier, and lets you use the page you wanted to submit to.

That said, even with this code, it will reload, and sometimes the last submissions will resubmit when you reload it. AJAX is the best way to resolve those issues, but this code allows you to just use PHP.

Farinose answered 14/12, 2023 at 1:40 Comment(1)
Note: This code WILL still reload your page.Farinose
W
-2

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"; }

Wareroom answered 24/7, 2011 at 9:33 Comment(2)
where showMessage() shows the req message at the required position on the webpageWareroom
You can traverse through DOM and use the values filled by user in showMessage() function. I think it will fix his problemWareroom

© 2022 - 2024 — McMap. All rights reserved.