Determining the Source of $_POST - php

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

Related

how can I get the value of a input field using PHP if there are two forms on the page both of POST method

I want to get the value of an <input> field using PHP.
There are two forms on my page both of POST method.
<form method="POST">
<input type="text" name="first">
<input type="submit" name="submit" value="submit">
</form>
<form method="POST">
<input type="text" name="second">
<input type="submit" name="submit1" value="Post">
</form>
How do I get the value of the second input field? Even though if I use $_POST['second'] it shows me an error:
Undefined index: 'second'
The W3C specs define that an input can be associated to only one form. It is a sign of bad design when you need to have multiple forms and the backend has to know which data is present in other forms.
The <form> element can contain arbitrary element structures like tables, it can even contain the entire document body content. You should hardly need multiple forms.
A common use-case is to have multiple submit buttons having the same name instead. Only the pressed button will be part of the form data.
<form method="post">
<input type="text" name="text_input">
<button type="submit" name="action" value="add">submit</button>
<button type="submit" name="action" value="update">submit</button>
<button type="submit" name="action" value="delete">submit</button>
</form>
Again, do not do that, however, if you really want to share a field across multiple forms for some reason, this can only be done by javascript intercepting the form submit event. This will not work when scripts are disabled by the user.
document.querySelectorAll('form').forEach(e => {
e.addEventListener('submit', function() {
document.querySelectorAll('.multi-form-input').forEach(e => e.setAttribute('form', this.id));
})
})
<input class="multi-form-input" name="common_input" type="text">
<form id="form-1" method="post">
<button type="submit" name="action" value="1">submit</button>
</form>
<form id="form-2" method="post">
<button type="submit" name="action" value="2">submit</button>
</form>

PHP Form to redirect with form results

I have a form that when they submit it, it stores the info in the database. I need to be able to get the form data to come up on redirect page.
It does not need to fetch the database as I would love to do this PHP style. So lets say they enter there name and city. When they click submit it redirects them to a thank-you page with the results from the form on that page.
In a form, you have each element have a name, lets say name="username", in the php, yould get the value of this as either a get, or a post response, depending on the method of the form.
HTML Form
<form action="process.php" method="get">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
or
<form action="process.php" method="post">
<input name="username" type="text"></input>
<input type="submit" value="Submit"></input>
</form>
process.php
$someusername = $_GET['username'];
$someusername = $_POST['username'];
Form page:
<form action="thanks.php" method="post"><input type="text" name="myname" placeholder="Name" /><input type="text" name="mycity" placeholder="City" /><input type="submit" value="submit"></form>
PHP Page
print "Thanks, ".$_POST['myname']." who lives in ".$_POST['mycity']."!";

A submit button to send data from one php function to another

I'm trying to figure out how to send data from one php function to another through using a submit button. The idea is that a spellchecker will highlight the correct spelling for a word, and the user will click the button to run the program again with the correct spelling.
<form method="post" action="results.php">
<input type="submit" value="Search!" name="submit" id="searchButton"
value="<?php
$search = "dog";
$_POST[$search]
?>"
/>
This is what I have so far...
Also, is it possible for the value of a button to be a variable?
You can use hidden variables as an alternative to ajax.
<input type="hidden" value="dog" id="search" />
<input type="submit" value="Submit" />

Sending additional $_POST data?

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

how to pass values from one page to another on jquery form submit

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.

Categories