GET variables not passed when mixed in with URL - php

Why is it that when I pass parameters through encoded URL and GET method in HTML form, the URL encoded parameters are dropped?
e.g. Setup:
<form action="process.php?hello=world" method="GET">
<input type="text" name="foo" value="bar">
<input type="submit">
</form>
Result: the variable hello will not be recognized in process.php.
Is this bad practice?
Is this how PHP processes it, or is it related to how the browser send the request? Is there the same problem in other languages?

Yes, that is bad practice because it just doesn't work.
If you want to pass in "hidden form input" then you must use a hidden form element:
<input type="hidden" name="hello" value="world" />
As rezzif states in his comment, you can mix GET & POST like so:
<form action="/something?foo=bar" method="POST">
<input type="text" name="baz" />
</form>
As a general rule I avoid mixing the two though. I find it bizarre to have GET params in my form action.

Related

Can not pass the action get param to the PHP file

I have a smarty project, and in the .tpl file, there is a form:
<form method="get" action="{$smarty.server.PHP_SELF}?action=func1">
<input type="text" name="username"/>
<input type="submit">
</form>
there is a question, if the php file have many function for different action requests, so in the template if have many forms, I want to through the action for distinguish.
but in my practice, see upper code, I write like this, this can not delivery the action to my php file.
I want to write the action in the form action, because this can be more standard. so I don't want to write it in a hidden input. why write in the action can not pass into the php file?
You could use submit button with specified name and value:
<form method="get" action="{$smarty.server.PHP_SELF}">
<input type="text" name="username" />
<input type="submit" name="action" value="func1" />
</form>
and then you'll get a global variable $_POST['action'] with value func1. But the value will be showing on your button title, so I offer you to find forms only by submit name, for example name='submit_form1'.

Trying to pass three parameters from one php file to another

I am trying to pass three parameters from one php file to another. Two of those parameters are in variables that are already determined long before the button is clicked to call the second php file, but one will be taken from a text box at the time the button is clicked.
So far I have the following (snippet) in the first php file. The two parameters that are in the existing variables show up in the URL just fine, but I can't figure out how to get the student number to be included. The URL just has "studentNumber=?&club=..."
Thanks!
<input type="text" id="studentNum" placeholder="Student Number">
<input type="button" value="Add Student" onclick="window.location = '<?php $url = 'http://npapps.peelschools.org/editor/add.php?studentNumber='.$_GET["StudentNum"].'&club='.$club.'&type='.$type.''; echo $url;?>'" />
Is it really necessary to use window.location? I would encourage you to use something like this
function doSubmit() {
document.getElementById("myformid").submit();
}
<form id="myformid" action="receivingPHP.php" method="POST">
<input id="studentnr" type="text" value="42" />
<button onclick="doSubmit()">Send</button>
</form>
Of course there is no receivingPHP.php file on the StackOverflow servers, so if you try this script you will reach a white page (close it in the top right corner where it says close)
If you use $_GET["StudentNum"], it must come from an HTML-form or a html-link:
example
or
<form method="GET"><input name="StudentNum" value="1337"></form>
Good luck
The URL of your current page needs to have had studentNum present as a query parameter to be able to use $_GET. For example, if current page URL =
http://npapps.peelschools.org/myotherpage.php?studentNum=100
then you can $_GET["studentNum"]. Also, if you are accessing this URL via ajax
http://npapps.peelschools.org/myotherpage.php
then it must be passed as a data parameter.
Find out what the URL of the page is where you have the HTML that you have shown, and if studentNum has not been passed as a query parameter or data parameter from however you get there (e.g. an anchor tag href) then add that parameter to the URL.
Ended up reworking it so that all the information was sent in a form rather than trying to embed it in a button. The secret came from w3schools where I figured out how to hide the known parameters in a hidden input element in the form, as follows:
<form action="add.php" method="GET">
<input name="studentNo" type="text" placeholder="Student Number" />
<input name="club" type="hidden" value="<?php echo htmlspecialchars($club); ?>" />
<input name="type" type="hidden" value="<?php echo htmlspecialchars($type); ?>" />
<input type="submit" value="Add Student" />
</form>

Form with multiple submit option. PHP

I have a HTML form page with following code :
<form action="chainresult.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET SEQUENCE" />
</form>
<form action="helix_info.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="submit" value="GET HELIX INFO" />
</form>
My page has two browse options and two submit options which takes the use to 2 php pages. I want to have only one browse option with two options that takes the user to 2 different php pages based on what the user clicks.
Any help is appreciated!
You will need to combine the two forms into one (you don't even necessarily need the form tags), use JavaScript or jQuery to capture the submit button click, evaluate the input value based on your validation rules that route the form submission, and then post the values to a form, likely through ajax.
You can submit multiple forms but you will have to use Javascript. It should be doable with jQuery without too much sweat and tears. Something like...
$("#my-submit-button").click(function(){
$("#first-form").submit();
$("#second-form").submit();
})
I am not sure I understand your question correctly, but if you want to be able to post data to two different URLs, with two different submit-buttons, having two different forms is the only way to do it with plain HTML.
However, it would be possible to use JavaScript. In that case you could mash both forms together, evaluate the input before sending any information, and the post the data to different URLs depending on the input, using AJAX.
It is worth noting that going down the JavaScript-road, you would make the form unusable for anyone who has disable JavaScript.
I would probably suggest that you make it a single form, point it to an URL that can handle either case. So you always post the data to the same URL, and the server-side code would have to evaluate the input and decide what to do with it. That case you don't eliminate users that doesn't have JavaScript activated.
Not sure if I understood the question properly, but you could use jQuery to change the action attribute of your form, depending on what the user chooses. Something among the lines of:
<form id="myform" action="dummy.php" method="post" enctype="multipart/form-data" />
<input type="hidden" name="MAX_FILE_SIZE" value="3145728"/>
<input type="file" name="userfile" id="userfile" size="30" />
<input type="radio" name="formtype" value="uploadscript1.php" /> Option 1<br>
<input type="radio" name="formtype" value="uploadscript2.php" /> Option 2<br>
<input type="submit" value="GET HELIX INFO" />
</form>
And in jQuery:
$('input[name="formtype"]').change(function(){
$('#myform').attr('action', $(this).attr('value'));
});
I am not sure about the jQuery part, but it should work ok. Try experimenting with that. :)
Using this approach you should be able to send the same data to two different forms.

Appending GET parameters to URL from <form> action

So say I'm currently on index.php or index.php?p=about within my current web build.
I am trying to build a search form that will be displayed on most pages, but I want the form action to go to http://mywebsiteurl.com/?p=search&q=GETDATA, as my website's paging depends on the data passed to the 'p' attribute.
How would I append the search parameter to the URL in a static fashion, upon submission?
Perhaps something like this:
<form method="get" action="index.php">
<input type="hidden" name="p" value="search" />
<input type="text" name="q" value="" />
<input type="submit" value="search" />
</form>
You can use a hidden field in your form to maintain the value of the p parameter:
<input type="hidden"
name="p"
value="<?= htmlentities($_GET['p'], ENT_QUOTES) ?>" />
You should put the value of the parameter p inside a hidden form field inside the search form; something like:
<input type="hidden"
name="p"
value="<?php echo(htmlspecialchars($_REQUEST["p"])); ?>" />
It's not a good idea to put the parameter to the form action parameter; post requests are handled differently than GET requests, the values in a POST request aren't appended to the URL by ? and & as with GET; meaning that you wouldn't actually get the p parameter into the script handling the POST request from the form...
Also take care not to show the request parameter unreflected (hence the htmlspecialchars, thanks for the hint!), since malicious clients could try to inject code into your page (HTML injection / XSS).

What is the purpose of $_POST?

I know it is php global variable but I'm not sure, what it do?
I also read from official php site, but did not understand.
You may want to read up on the basics of PHP. Try reading some starter tutorials.
$_POST is a variable used to grab data sent through a web form.
Here's a simple page describing $_POST and how to use it from W3Schools: PHP $_POST Function
Basically:
Use HTML like this on your first page:
<form action="submit.php" method="post">
Email: <input type="text" name="emailaddress" /> <input type="submit" value="Subscribe" />
</form>
Then on submit.php use something like this:
<?
echo "You subscribed with the email address:";
echo $_POST['emailaddress'];
?>
There are generally 2 ways of sending an HTTP request to a server:
GET
POST
Say you have a <form> on a page.
<form method="post">
<input type="text" name="yourName" />
<input type="submit" />
</form>
Notice the "method" attribute of the form is set to "post". So in the PHP script that receives this HTTP request, $_POST[ 'yourName' ] will have the value when this form is submitted.
If you had used the GET method in your form:
<form method="get">
<input type="text" name="yourName" />
<input type="submit" />
</form>
Then $_GET['yourName'] will have the value sent in by the form.
$_REQUEST['yourName'] contains all the variables that were posted, whether they were sent by GET or POST.
It's used to store CGI input via a POST sent to your page.
Example:
Your page contains:
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
One the user submits the values input into the form, you can access those variables through $_POST using the names you provided for the input tags.
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
You can capture post values from forms:
Example:
<form method="POST">
<input type="text" name="txtName" value="Test" />
</form>
To get this you'll use:
$_POST["txtName"];
It contains data sent by HTTP post, this is most often from a HTML FORM.
<form action="page.php" method="post">
<input type="text" name="email" ...>
...
</form>
Will be accessible by
$_POST["email"]
It contains the data submitted via the POST method, and only the POST method, versus data submitted via the GET method. The $_REQUEST superglobal variable contains both $_POST and $_GET data.
When data is posted through a form to the server, you access it through the $_POST array:
<form method="post">
<p><input type="text" name="firstname" /></p>
<p><input type="submit" /></p>
</form>
--
<?php
if ($_POST)
print $_POST["name"];
?>
Not all data is sent through $_POST through. File uploads are done through $_FILES.
As defined by the Hypertext Transfer Protocol specifications, there are several types of requests that a client (web browser) can make to a resource (web server).
The two most common types of web requests are GET and POST. PHP automatically loads any client request data into the global arrays, $_GET and $_POST, based on the type of web request received. The type of request is transparent to the user of the web browser, and is simply based on what is going on in the page. In general however, any regular link you click produces a GET request, and any form you submit produces as POST request.
If you click a link that goes to "http://example.com/index.php?x=123&y=789", then index.php will have it's $_GET array populated with $_GET['x'] = '123' and $_GET['y'] = '789'.
If you submit a form that has the following structure:
<form action="http://example.com/index.php" method="post">
<input type="text" name="x">
</form>
Then the receiving script, index.php, will have it's $_POST array populated with $_POST['x'] = 'whatever you typed into the textbox named x';
There are two ways of sending data from a form to a web app, GET and POST.
GET sends the data as part of the URL string: http://www.example.com/get.html?fred=1&sam=2 is an example of what that would look like. There are some problems with using it for all processing, one of the biggest is that every browser has a different maximum length for the query string, so you may have your data truncated.
POST sends them separately from the URL. You avoid the short length limit, plus you can send binary or encrypted data with POST.
In the first example above, PHP can retrieve the values sent by $_GET['fred'] and $_GET['sam']. You would use $_POST instead if the form was POSTed.
If you're wondering which method you should use, start here
$_POST is used to retrieve values passed to your page via a POST request.
For example, your page uses a form to pass data to another page in your application. Your form would have
<form method="post">
to pass those values via POST.
It is matched by $_GET which perform the same function for GET requests.
If you want to be able to reference either GET/POST values, you can use $_REQUEST
It contains any values posted from a HTML form to this script.

Categories