I use a form to send some data to my welcome.php file:
<form action="welcome.php" method="post">
Name: <input type="text" name="txt" />
<input type="submit" />
</form>
But, is it possible to send other variables instead of just form element values? Let's say I want to send the number 100 to welcome.php as well.
You would probably use a hidden input:
<form action="welcome.php" method="post">
<input type="hidden" name="myNumber" value="100" />
Name: <input type="text" name="txt" />
<input type="submit" />
</form>
That would send the myNumber $_POST value to welcome.php.
Modifying the action like this: welcome.php?myNumber=100 would mean that you are sending a GET variable in addition to the POST variables inside the form.
NOTE: You could theoretically use both, but I believe that would only put the $_POST value in the $_REQUEST object. You should confirm that before relying on that behavior.
If I understand correctly:
<input type="hidden" name="myNumber" value="100" />
Right?
other then hidden fields or form element you can use AJAX to send other variables into the post.
where you can pass url with your new variables other than form fields.
Thanks.
You can hardcode that into your action:
<form action="welcome.php?number=100" method="post">
This should work:
<form action="welcome.php?number=100" method="post"/>
or use a hidden form input
<input type="hidden" name="number" value="100" />
You can use html hidden field or store the value what you want in session or cookie varible
Related
Is there an easy way to find out the source of a post variable in PHP?
Form on example.com/formone.php
<form method="post" action="test.php">
<input name="myusername"type="text">
<input name="mypassword" type="password">
<input type="submit">
</form>
Form on example.com/formtwo.php
<form method="post" action="test.php">
<input name="myusername"type="text">
<input name="mypassword" type="password">
<input type="submit">
</form>
I understand that I could use a hidden input, but I was wondering if PHP had a method to test the source of the POST.
While you could potentially use the HTTP_REFERER server variable, it is not very reliable. Your best bet would be to use a hidden field.
Another alternative would be to switch out your submit input for a submit button. This way you can pass a value with it, retain the button's label, and test for that inside your test.php page:
<button name="submit" type="submit" value="form1">Submit</button>
In your PHP file you would then test:
if( $_POST['submit'] == "form1" )
// do something
Is it possible for me to have two self submitting forms on a single page. If yes how do I allot different blocks of code to each form ?
Have a hidden input with two different values.
<form action="" ...>
<input type="hidden" name="form_no" value="0">
...
</form>
<form action="" ...>
<input type="hidden" name="form_no" value="1">
...
</form>
On the server side, different on the basis of $_REQUEST['form_no']
Or you could also add it as a name parameter in submit element.
<input type="submit" name="form0">
Use isset($_REQUEST['form0']) to differentiate.
Another way of doing it is to append a GET parameter to differentiate
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_no=0" ...>
...
</form>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_no=1" ...>
...
</form>
Use $_GET['form_no'] to differentiate.
Name your first form form_one, and the second form form_two.
<?php
if (isset($_POST['form_one'])) {
// First form was submitted
}
if (isset($_POST['form_two'])) {
// Second form was submitted
}
?>
Two forms can be placed in a code by giving them different names and keeping their target _blank
<form action="action.asp" method="get">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
<input type="submit" name="submit1" value="Submit" />
</form>
<form action="action.asp" method="get">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
<input type="submit" name="submit2" value="Submit" />
</form>
<?php
if(isset($_GET['submit1'])){
// first form was submitted
}
if(isset($_GET['submit2'])){
// second form was submitted
}
?>
Each form has a different script specified in this example, but keep in mind that you can simply use the same php file for both actions, specifying a different block of code for each form (that's the php part above).
Someone may have a better answer, but this method has served me well in the past.
Following is my sample form.
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="text" NAME="array[]" />
<input TYPE="submit" NAME="submit" VALUE="Submit" />
</form>
Basically I have 10 inputs of array. Assume my domain is http://domain.com and the file above is index.php. I am trying to fill the form automatically by using the following method.
http://domain.com/index.php?array[]=John&array[]=Kelly ... & array[]=Steven
Unfortunately, it is not working. :(
Have you tried something like this:
<?php
foreach( $_GET['array'] as $arr ) // Loop through the `array` variables of GET
echo '<input type="text" name="array[]" value="' . $arr . '" />'; // Display the the inputs
?>
However, please make sure that you use a cleaning function on $arr to prevent XSS. You will also need to check if $_GET['array'] is set or not, or PHP will whine about it.
Two things to try.
You are using the GET method with your example URL, but your form is set to POST. In your PHP, you may be looking for your data in $_POST when it's actually in $_GET. You can get data from both POST and GET using the $_REQUEST variable.
You may need to urlencode the []. ([] becomes %5B%5D when urlencoded.)
Lastly, a tip: when crafting links to send data through a GET query string, it is best practice to use & in place of the &'s in the URL.
If you are trying to fill the form from a GET request (your URL string), you have to get the values from the HTTP request in the $_REQUEST array (or $_GET or $_POST depending on the form method). Suggestion: you should change the names of the form fields to reflect the value they are storing
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="first_name" value="<?php echo $_REQUEST['first_name']?>"/>
Note: accessing the $_REQUEST array as shown above is not good practice as you need to check if the request variables are set before you can echo their values.
<form METHOD="post" METHOD="post" ACTION="index.php" METHOD="post" METHOD="post" METHOD="post">
<input TYPE="text" NAME="first_name" value="<?php echo isset($_REQUEST['first_name'])?$_REQUEST['first_name']:""?>"/>
Is there a way to get the value of the name attribute in the form tag? I'm using PHP and don't see it in $_POST.
Is there a way to get the value of the name attribute in the form tag? I'm using PHP and don't see it in $_POST.
No, the form's name attribute is never set to sent to the server as part of the POST data.
The easiest way around this would be adding a hidden form element <input type="hidden"> containing the name.
<form name="myform" method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="frmname" value=""/>
</form>
<form name="wut">
<input type="hidden" name="name" value="wut"/>
</form>
I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.