How to submit additional variables alongside form submission? - php

I have an HTML form that works properly and stores all variables ('firstname', 'lastname', etc.) in the GET array.
At the time of form submission, I need to pass an additional variable in the GET array to let the PHP controller know to display the next set of prompts. Something simple like step=2.
What I tried doing was setting the form action to ?step=2 as well as ?step=2&. But, no matter what I try, the form's variables are all that show up in the URL.
What am I doing wrong? Is this even possible?

You can use a hidden form field in your form:
<input type="hidden" name="step" value="2">

$_SESSION['step'] = 2;
PS: Or a session var XD
Saludos.

Related

PHP Passing a variable to another page then pass it back to the original page

This is a little bit quite confusing to explain. I want to know how to store a string into a variable and pass it to the next page and in that next page pass that variable again to another variable then that variable will pass it back to the original page.
Example:
I have two PHP pages. lets call them form1.php and form2.php
In form1.php, I will input Hello and it will be stored in var1.
var1 will be sent to form2.php and it is stored in var2 in form2.php as well.
in form2.php, var2 is passed to var3 and var3 will be sent back to form1.php.
And output Hello in form1.php using var3 not var1.
This is where I got stuck. Can't figure out how to do this. Please comment if the question is a little vague, even I cant seem to figure it out myself. Thanks.
You want to use Sessions. With sessions you can pass variables to a different page.
You have to start sessions on both pages.
session_start();
After that you can store variables in a session.
For example:
$var1 = $_SESSION['name'];
Now you can use IT in every page with session_start();
Well if you are actually using forms then you should be able to just grab the values of the variables in $_GET or $_POST (depending on the method used by the forms). You could just store the values in hidden inputs the user doesn't see and use that to pass things around:
<input type = "hidden" value = "<?php echo $_GET['var2'];?>" />
If this isn't what you need you may want to look at sessions to maintain state throughout your site.
My understanding is that you want to pass some data between two distinct forms - you need to consider that these forms are completely independent form each other so you will need to pass data back to the browser or leverage a server side session.
Depending on your use case you could:
Submit data to form1.php - this would then return a form with additional fields ready for submision to form2.php
The trick here - is that data that needs to be passed between forms would be included in the generation of the second form as hidden elements.
<input type="hidden" id="var1" value="data from form1" />
You can have as many hidden types as you need.
Be aware this approach is not very secure - so you may need consider defences e.g. csrf - or using sessions and tracking the data on the server side.
From what I understand, you need two forms to send data from the first to the second and from the second back to the first. In this communication, you don't need the third var, how about this?
form1.php
<form method=POST action="form2.php">
<input type="text" name="var1" value="<?=#$_POST["var2"];?>">
<input type="submit" value="Continue">
</form>
form2.php
<form method=POST action="form1.php">
<input type="text" name="var2" value="<?=#$_POST["var1"];?>">
<input type="submit" value="Continue">
</form>

When submitting a web form, don't reset data fields

How can I submit a form to itself without clearing the data in the fields using HTML, javascript and PHP?
You could take different approaches (e.g. cookies, jquery, etc...), however HTML + a line in PHP are more than enough in this case. Try this example code:
<form name="test" method="post">
Your Name: <input type="text" name="YourName" <?php if (isset($_POST['YourName'])) echo 'value="'.$_POST['YourName'].'"';?> >
<input type="submit" value="Submit">
</form>
In the code above if something has been posted to the receiving page (that can be the same page, such as in your case), then the posted value is printed out in the corresponding field. You can use this approach for all the fields composing your form.
If you want, you can also use similarly the $_GET method in the form.
If you use the traditional form submit, you need to save the parameters and rewrite the form input elements when you write the form the next time. But a better way is to use AJAX -- then the field data is sent without a form submission, and the input elements retain their data. See this link: http://www.w3schools.com/ajax/default.asp

Remember form value when return back to submit due to some error

After filling the form when submit, accidentally due to some filling error ,the form is not submit and return to back,in this condition the value of all text box is blank. i want to stable value of all fields in this condition . I'm using php with smarty framework. Please reply with solution as soon as possible.
Thanks.
If the form is submitted to the page that contains it then you will have access to the submitted values, and can use them to populate your form. For example, if you are submitting the form via POST:
<input name="something" value="<?=$_POST['something']?>" />
If you are submitting the form to a different script, you could send the values back to the page with the form as URL parameters, or you could use temporary session variables, and unset them when the input passes whatever validation you are using:
$_SESSION["temp_something"] = $_POST["something"]; //In form processing script
Then in your form:
<input name="something" value="<?=$_SESSION['temp_something']?>" /> <!--In form-->
You can fill the form fields, on the second round, by filling the content inside the value attributes of html tags, like so:
<input type="text" value="<?php echo $_REQUEST['test']; ?>" name="test">
Pay attention: this is a fast and simple solution. It gives you an idea. In good web programming practice you should sanitize the form data received by client in order to avoid security issues.

Is this a nice way to preserve a field value when an HTML form submitted?

I have a form with two submit buttons.
The user fills field-A and hits submit.
Done that, some input fields will be filled with data.
After that first submission, the value on the field-A should not disappear.
How can we preserve this value after the first submission?
Should we, on the field-A value attribute, place:
value="<?php echo isset($_POST['fieldA'])) ? $_POST['fieldA'] : ''; ?>" ?
The form submits to self.
Update - Additional details:
This is a form that will have two submit buttons on the same page (sort of speak).
Submit Button A - Will grab some data based on a input field, and fill the other input fields on that form.
Submit Button B - Once the form is filled, it will use all that data to do another submission.
This is a very simple case, no frameworks are in place here. I do have, however, some sort of MVP structure here.
Thanks in advance,
MEM
In general, such things being done using 2 forms, no one.
And GET method, not POST. At least for the first form.
But as you cannot ask a question, it's impossible to give you an answer.
Here you go:
index.php
<form action=edit.php>Enter name: <input name="name"><input type=submit></form>
edit.php
<? $row = dbget("row","SELECT * FROM domains WHERE name = %s",$_GET['name']); ?>
<form method="POST" action="save.php">
Edit data for <?=htmlspecialchars($row['name'])?>:</br>
NS: <input name="ns" value="<?=htmlspecialchars($row['ns'])?>"><br>
Another: <input name="another" value="<?=htmlspecialchars($row['another'])?>"><br>
<input type="hidden" name="name" value="<?=htmlspecialchars($row['name'])?>"><br>
<input type=submit>
</form>
save.php
do whatever you do usually to save info
I would store these values into $_SESSION, as user fabrik said. This way they can be stored across the entire form submission process(assuming it is multiple pages) and posted all at once at the end.
Assuming you're having some kind of submission system with a "next" button to go to the next set of forms, using session_start() and $_SESSION is certainly the best method. More information could be found here, or various tutorial sites--
http://php.net/manual/en/reserved.variables.session.php
It's ok to do that with $_POST, some people dont like the ternary operator but for me it works just fine. Although, there are better ways to deal with forms using O.O.P. You could create a class that manages your form, and pass an array to the constructor of that class (eventually you could pass the $_POST) and the class will create your form according to the info submited. You could even use the same class to valdidate your form
I don't see the need of using $_SESSIONS, cause this is not information that you need to preserve during the whole session.. or not?
Try this:
<?php
$fieldA = (isset($_POST['fieldA']) ? $_POST['fieldA'] : '')
?>
// and in your form
<INPUT type="text" name="fieldA" id="fieldA" value="<?=fieldA?>" />
as you mentioned, this should work.

php custom post variables

is there a way to create custom post variables when a user presses submit, like this:
$_POST['var'] = 'hi';
In order to set post values on the page with the form you should use hidden input tags.
i.e.
<input type="hidden" name="var" value="hi" />
It will be invisible and your receiving script will see that key/value passed along.
Variables POSTed by the browser to your PHP script will only correspond to the fields of the form that was used in the browser -- which means you have to put your custom data in that form.
If you don't want them displayed, you can use a hidden input field :
<input type="hidden" name="var" value="hi" />
But note that the data will still be sent by the browser -- which means you have to escape/filter/protect it, like any other value that comes from the user ; and it cannot be trusted : anyone can pretty easily modify the value of that form field, even if it's not visible.
while $_POST variable is an array, you can also define var like this
$_POST['var'] = 'hi';
it is same like hidden field. :)

Categories