php custom post variables - php

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. :)

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>

How to submit additional variables alongside form submission?

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.

How can I populate the fields of a PHP form automatically when the field values are in the url?

I have a have in PHP and I have common fields such as 'Name' and 'Surname'.
Now when the user visits the page e.g. http://www.example.com/form.php the form fields 'Name' and 'Surname' are empty.
I would like to now have a link similar to this http://www.example.com/form.php?name=John
so that when the client hits the link the PHP form will now have the name field already filled with 'John' in it.
I know this can be done in HTML but how can I do it in PHP?
Just to let to know I do not own the PHP form - I just want a link from my website to fill the PHP form (which I do not have control over).
Thanks in advance.
Can be done using $_GET
An associative array of variables passed to the current script via the URL parameters.
e.g.:
<? php
if(isset($_GET['name']))
{
$test = $_GET['name'];
}
?>
<html>
<body>
<form>
<input type="text" name="test" value="<?php if(isset($test)){echo "$test";}?>"/>
</form>
</body>
</html>
Note: code isnt tested or anything.. Also, there are possible security risks with getting values from your URL (can be considered user input), so make sure you are aware of that and how to prevent
You could store that value and then when you're about to output the input fields
you just pass along the stored value.
$name = $_GET['name'];
// ... later on
echo '<input type="text" value="'.$name.'"/>';
By using $_GET superglobal
<input name="name" value="<?php echo !empty($_GET['name']) ? $_GET['name'] : '';?>" />
<input name="surname" value="<?php echo !empty($_GET['surname']) ? $_GET['surname'] : '';?>" />
You can use the get method in php to get the name and make use of it
You can retrive this information by the $_GET["name"] function, or $_REQUEST["name"].
Reserver variables
Be carefull with those operations, you might have validation a/o security problem.
Note: if you are not sure that the "name" variable is set or not, you have to use also the
isset function to test it.
You can use the $_GET superglobal, so your input could look like this:
<input type="text" name="name" value="<?php if(isset($_GET['name'])) { echo $_GET['name']; } ?>" />
The $_REQUEST superglobal does a similar thing but I would just use $_GET.
It looks like everyone's answers here assume you are building the form yourself, which doesn't appear to be the case based on your question.
The thing that you want to do may or may not be possible. If the form accepts certain kinds of parameters in certain ways, you may be able to hook in to that functionality and set it up so that when someone clicks a link on your page, that information gets passed to the other page.
One way forms can accept this information is in the form of a "get" request. With this method, values are passed as part of the url, as in your example: http://www.example.com/form.php?name=John. Assuming your page has access to a php variable called $name, you can create a link from your code to build this kind of url like this:
Sign up!
If the page does not accept get parameters in this way (and I have a hard time imagining that they would), you may have to try other techniques to send along the information (assuming that they will even accept it!). The two other ways I imagine you could do this are by passing the value with "post" or creating a cookie for the page. If you tell us what page you are trying to set up this behavior on, we might be able to examine it and give you a better answer.

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.

Categories