What is this code doing? If Isset, and what are the variables doing, whats the $_POST doing? Can someone explain please?
if (isset($_POST['Submit'])) {
$title=$_POST['title'];
$forename = $_POST['forename'];
$surname=$_POST ['surname'];
$dateofbirth=$_POST ['dateofbirth'];
$gender=$_POST ['gender'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$passcode1=$_POST ['passcode1'];
$passcode2=$_POST ['passcode2'];
$_POST contains the data from the form the user submitted.
if(isset($_POST['Submit']))
is checking to see if a form was submitted. There is likely a hidden element with this name, or the submit button was created to include this value.
The remainder of the lines copy*1 the information out of the POST super global into easier to use variables.
(*1 not actually copied, PHP is copy on write)
If a form is being submitted, then it assigns variables.
It will check If the form is submitted set the value of html elements in to the PHP variables that's it.
These variable can be used for further processing and later to store in database.
So here is an attempt to answer your amorphous question.
if (isset($_POST['Submit']))
This line is checking to see if an entry called 'Submit' is present in the $_POST array.
isset is a function that checks the existence of variables in PHP.
$_POST is a global variable in PHP that captures the key-value pairs sent in an HTTP POST request. $_POST will contain the result of an HTML form submission in 99% of the use cases.
$title=$_POST['title'];
is setting the title variable in the PHP script to the title field in the post array. title was probably a text field submitted by a form.
Looking at the use of $_POST in your example code, the request to the php script probably comes from a form that looks like:
<form method="post" name="someForm" action="thatScript.php">
<input type="text" name="title"/>
<input type="text" name="forename"/>
<input type="text" name="surname"/>
... other inputs that correspond to $_POST entries...
<input type="submit" value="Submit" name="Submit"/> <!-- the submit field that is checked in the isset -->
</form>
Notice: <form method="post"
This tells the browse to submit the key-value pairs for the form in a post request. If that line read <form metho="get" then the browse would submit the key-value pairs as a get request and the php script would check and use $_GET instead of $_POST.
It checks if the form has been submitted and if its true then values from post array (that are sent by the sending page) are assigned to variables.
for example value for this $_POST['title'] is assigned to variable $title.
Related
Is it possible for a user to enter a value into a form and then, on submit, have the page redirect to a new one with the value entered into the form stored in a PHP variable?
This if my form code;
<form id="loc-search" method="post">
<input type="text" id="search-by-location" name="custom-location" value="" placeholder="Sheffield, UK"/>
<input type="submit" id="submit" value=""/>
</form>
Once the user has entered a value in #search-by-location the page needs to redirect to weather.php with the value stored in a PHP variable called $location
AJAX / JS aren't my strong suits so if someone could point me in the right direction that would be great
Add the argument action="weather.php" to your form tag. Then, when clicked on the submit button, you will get redirected to that page. Depending on your method, in your case POST, the input values will be available in the superglobal $_POST array in PHP.
In your example, $location = $_POST["custom-location"]; will suffice. Note that the name, not the ID, determines the array key in the target PHP document.
Javascript or AJAX are not needed to achieve this.
This is just a normal form so why not just use $_POST after the redirect on the weather.php page:
$location = $_POST["custom-location"];
As #Tacticus pointed out you also need to have the form redirect (if you did not already do this in JS). By adding action="weather.php" in the form:
<form id="loc-search" method="post" action="weather.php" >
...
</form>
As stated in other answers you should modify your form to look like this:
<form id="loc-search" method="post" action="weather.php">
<input type="text" id="search-by-location" name="custom-location" value="" placeholder="Sheffield, UK"/>
<input type="submit" id="submit" value=""/>
</form>
In your weather.php file, you can get the value from the $_POST global variable, just like this:
<?php
$location = $_POST["custom-location"];
//Interpret data
?>
Note that you can access the value of an input tag from your form witch was passed, with the input's name. In html you specify the following:
<input name="yourname" />
And when you want to access that value, you simply refer to his name.
$_POST['yourname']
If you use GET method for the form to pass the values, then you do the same, only the value will be stored in the $_GET global variable, so in your case with a GET method the variable initialization would look like this:
<?php
$location = $_GET["custom-location"];
//Interpret data
?>
I have my index.php page that sends values over to edit.php via $_GET/html link. I can see the values go, including the table ID I want to use using echo statements. The problem is that I need the specific ID value used in another form using a submit form($_POST). I've tried different approaches including SESSION, making the $ID = $_GET.... I'm pulling my hair out.
Here is the order of things:
//Edit link on index.php sends the id='summary_id', which works
<td align="center">Edit</td>
//Edit.php grabs the variable and I set it to that variable
if(isset($_GET['id']))
echo "<pre>Value of GET \$_GET:</br>";print_r($_GET);echo"</pre>";
{
$summary_id = $_GET['id'];
$summary_id = $_POST['summary_id'];
But, when I try to use $summary_id in a form (using $_POST this time), I get no value. I'm not sure if what I'm trying to do is "legal". I've tried sending the summary_id as a hidden value in the form, but again, nothing is going through. I'm losing it after that initial $_GET.
You are doing wrong.You are overwriting the variable that is not available $_POST['summary_id']
if you really want to use post method use this
<form action="edit.php" method="POST">
<input type="hidden" value="<?php echo $row['summary_id'];?>" name="summary_id"/>
<input type="submit" value="EDIT"/>
</form>
or you can use header() to post the value
You are overwriting your own variable. Also, it's clear from this that you don't actually have PHP errors turned on.
You could use $_REQUEST instead as it contains both $_GET and $_POST.
HTML:
<form name="myform" action="process.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
<!-- ........ -->
</form>
PHP:
if (array_key_exists('check_submit', $_POST)) {....}
Why can array_key_exists('check_submit', $_POST) check whether the form was submitted?
I've seen isset($_POST['...']) used before, but not this.
if i don't do this array_key_exists('check_submit', $_POST) decision., what may happen.
To check wether your page was called as a result of a form submit (via POST) you should use something like this:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
You shouldn't use hidden form elements just to have a value to check wether a form was submitted. If you use the request method you catch it more cleanly and don't have any trouble if the form ids/names/values are altered.
check_submit is a field in your form, so when you submit the form, that field is available in the POST data.
PHP places incoming POST-method form data into the $_POST superglobal array, and your code determines whether the check_submit field can be found in that array.
Indeed, it's quite similar to isset($_POST['check_submit']), in that it checks whether such an element exists in $_POST. It's just taking a slightly different approach.
If you did not submit the form, then of course there is no form data.
Do var_dump($_POST), you will see the $_POST associative array, which will have check_submit key, and value = 1
Don't you mean something like:
if(!empty($_POST) && isset($_POST['check_submit']) && $_POST['check_submit'] == '1'){
// do something
}
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 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. :)