how to pass post data back to the form - php

i'm doing a form submit. i've got these 2 files.
1) php html form
2) php file which receives post data from #1
the process will have to be like this:
1) user go to php html form, key in some data and click submit to post data.
2) the php file will receive the post data and process it.
3) after processing the data, i would need the php file to return the post data back to the php html form and populate the textbox.
how can i do this? i understand we can normally post to PHP_SELF and process it in the same php html form but i would like to use a separate php file to receive and process post data.

Since you're using a seperate PHP file to process the post, and using redirects to get back to the original page, you'll need some way to pass the data. You've got 2 main options:
Posting directly to the seperate processing page, and redirecting back to the form page.
Posting via AJAX and returning the textarea data as an AJAX response
Option 1 is the easiest and quite doable, but you'd need to store the data somewhere while redirecting. Storing it in the $_SESSION array is the best bet. You could stuff it into a cookie or pass it via query argument, but both are hideoulsy bad ideas (just mentioning them for completeness).
Option 2's more involved, and you need to build some infrastructure into your page to handle the AJAX responses, page updates, and how to handle error conditions and display appropriate message ("YOu didn't fill out this field", "that's not a valid credit card", etc....), but saves you having to redirect and store the data between the requests.

it would use javascript to change object values
for example:
input1.value = ``

Related

format and resend form data from PHP script as if it came from an HTML form

Is there a way that I can have a form that submits (via POST) a form element to a PHP script on my server... then have the PHP script re-encode the data and send to another server, as if it came from an HTML form?
I basically need to intercept one of my forms that users will use, so that I can record data (which are lists, nothing confidential or personal information) that the user selects in the first form, then it will be processed by my PHP script, but then I want it to be re-encoded as if it was coming directly from the original HTML form (as I have to submit some hidden fields to the final destination server after my PHP script works with the POSTed form data.
How can this be done? and if someone could give a simple example, I'd appreciate it. Thanks

Trigger a function upon receiving POST

Is there a way to trigger a function when a $_POST is received?
I have a php page that finishes executing and calls another page. The other page performs certain actions and POST updates back to this page. I need to be able to update a div as and when a POST is received.
Making an ajax request and using "success" callback as a trigger doesn't work since this will update my div ONLY ONCE. The problem is that this page will receive POST multiple times at irregular intervals and I need a way to trigger an action whenever POST is received.
Use Ajax (I'd use jQuery) and set a time interval to check if there was a status change. The "other page" should not post back to the first page, but save data in a "requestable" area (might be an database, a file etc) where this new info/status will be stored and retrieved periodically by the Ajax request.
Use AJAX, because you can't trigger $_POST recieving in php,
As I understand you just need to update some <div> inner html after post is recieved, and you are able to do it with jQuery events
I would do this in the following way (below is the PHP code, just FYI):
if (!empty($_POST)) {
// do whatever you mean by 'updating the div'
}
However AJAX may be more appropriate and it is (believe or not) more flexible.
AJAX call will be smaller load on your server and can update the box single or multiple times (depending on how you write the JS code) - in such case I recommend using jQuery for simplicity (if you are not familiar with JS).
usually, I would do something like this...
$var_posted=0;
if($_POST['var']){$var_posted=1;}
then...
if($var_posted==1){some_function();}

Using same php file to show form and receive the form's data

I have a php page that generates a form. The action attribute of the form is the page itself. After the user submits the form, the same page is loaded, but this time a POST variable is set, so the page runs another script to deal with the incoming data from the form. I do this by using a conditional fork:
if(isset($_POST['var'])){
generate form
}else{
insert $_POST data into database
}
I'd like to know if this is ok or a bad idea.
I agree with Ignacio. Other than that it looks like a fairly standard approach if you don't need more complexity. One very important thing: make sure you are validating and sanitizing that data before it goes into the database.
The bad part is setting the action attribute to the script. Omitting it completely indicates to the browser that it should be posted to the same URL.
You might even want to go to the extent of checking whether the data was submitted thru AJAX to differentiate it from a regular form submission:
if ( $_SERVER['X_REQUESTED_WITH']=='XMLHttpRequest' )
// AJAX

PHP: Pass non-form variables between pages?

I have a page. The user submits the page and sends it to a PHP results page. It works fine. Now, I want the results page to link to another page, and for the elements on that page to depend on what was on the results page. I know how to pass form variables to another page, but I don't know anything about passing non-form variables.
From my searching on the web, my best guess is that I should be passing by URL. Is this correct? If so, a possible problem: the page I want the results page to pass to will have a form, and the user will go to yet another results page by clicking submit (the form data will be sent by POST). Can I send the non-form data (the old results page variable) along with the form data, if the user is going to the other page using POST?
I strongly suggest using sessions. It's not that hard to learn, php makes it VERY easy using http://php.net/session_start and the $_SESSION variable.
Advantage is that you will not have to submit a form on every click, and no information will be displayed in plain text in the URL.
There are several options. However, the easiest may be to simply pass the data on using hidden input fields.
Other options would be using the session, storing to a database between forms, or some combination therein.
If you are going to use POST to go to the next page the most simple option is to include the data you want to send along using an input type="hidden" element in your form.
You might consider using a session to pass the data along.
You can embed the non-form data into the second form as hidden fields. To make sure that it is also passed in the link, you can just add it to the URL as a query string, like:
http://..../blah.php?var1=val1&var2=val2
as long as the data you're passing can fit into a URL. Be sure to urlencode() anything you're appending to the URL.
<?php
start_session();
$_SESSION['Foo'] = 'Bar' // Add stuff here.
?>

Php, passing data between pages without using the url?

I have a php page that has a form that asks for an e-mail. When you press the send button, it gets to another php page, which gets the form data and does its stuff. I need to then be able to go back to the old page (the one that contained the form) and give it some data so that it will be able to change itself and say "You've sent your e-mail successfully, and will not display the form.
How do I do it?
Sessions probably
http://us2.php.net/manual/en/book.session.php
You can either use sessions or cookies, to not depend on the URL cookies have always to be enabled.
Check the PHP Manual (Sessions and Cookies).
Options:
1) Set a cookie (or use a session variable, which is kind of the same thing)
2) Use a separate thank-you page. After you've processed the form, redirect to http://www.mysite.com/thankyou
3) Process the form on the same page as itself. If your form is at http://www.mysite.com/myform, then at the top of that page have a little
if ($_POST)
// process form
// display thank you
else
// display form
Good luck!
If the user is just seeing data that they've entered anyway, you can just use hidden form fields:
<input type="hidden" id="lang" name="lang" value="en" />
That way you can continue to POST new forms and pass the data down the lane. That's the easiest thing to do without having to write a single extra line of PHP code.
You could also store each section in a database and save each section as-added. That would give you the added benefit of having partial data in the case of a browser crash or whatever, depending on how many parts your form is. You could then pass just an ID to the DB table row and retrieve the data for display.

Categories