JLA
JLA

Reputation: 329

PHP - Avoid reloading page after form submit

I'm new to php and web-programming... I'm working on a school project modifying databases through a corporate site.

I'm using some forms to allow the user to look for information that will be used to fill up other forms on the same page. In this case, a car rental, I look for available cars, show them on a table and then the user picks a car and its info would fill up some other inputs on the same page.

I'm able to do the search and show the result, but then when the user picks the car and clicks submit the whole page is uploaded again. Please, any suggestions?

J.

Upvotes: 2

Views: 3373

Answers (3)

Starx
Starx

Reputation: 79049

Redirect to a different page/route, other than the one page was submitted to.

An example for different page redirection.

header("location: differentpage.php");
exit;

Once you are in differentpage.php you cannot reload the POST request. TO get the data you can use SESSION or GET parameter as per requirements.

Upvotes: 2

Wicked
Wicked

Reputation: 163

If you don't want to use javascript and ajax you can use an iframe and set the target of the form as the iframe's name

note that you have to write 2 different pages to achieve this:

  1. One that creates the form and the iframe
  2. One that display data and uses posted values

Code in index.html

<form action="test.php" target="tar">
    <input type="submit"/>
</form>
<iframe name="tar">
</iframe>

Code in test.php

var_dump($_POST); // show your data

Upvotes: 1

Johan
Johan

Reputation: 317

You need to do this in JavaScript, not in PHP.

PHP is a server-sided language, what you do is parsed on the server side.

To stop a form submission you need to use JavaScript. Set an onSubmit event on your form..

Example code:

<script>
  function validation()
  {
    // do stuff

    return false; // stops submission of form
  }
</script>
<form onsubmit="return validation();">
  <input type="submit" value="Submit" />
</form>

Upvotes: 1

Related Questions