Im try to Programm some php.
i have a site : /index.php?go=newstep&callid=2
Where i put:
<form method="post" action="addnew.php"> <input type="text" name="user" /> <input type="text" name="text" /> <input type="hidden" value="<?php echo($_GET["callid"]); ?>" name="test" />
This is because the next site "addnew.php" Needs the value "callid" from the link to ?go=newstep&callid=2
Why isn't it working?
Is there another way?
Thank you
If You want to use GET method, You can simply put Your variable as part of the link in action attribute. You don't have to use hidden input. Something like that:
action="addnew.php?callid=<?php echo $_GET['callid']; ?>"
Additionally, '"' char in attribute "value" may cause problems, because HTML may interpret it as end of the attribute value.
EDIT:
Exactly, You are using POST method in form, thus You are sending You variable, callid, by POST method now and it will be available in $_POST global array in addnew.php script, not in $_GET global array.
Related
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>
So I have a form where one entry is:
<input type="text" id="ptid" name="ptid" readonly="readonly" placeholder="<?php echo $pid; ?>">
The value of "$pid" is not null, I already got the value from database. Then I would like to get that value and pass to another php file. So I tried this code :
<?php
$ptid=$_POST['ptid'];
?>
I tried printing this out, but somehow there's no result. Is there anyway to get the value?
placeholders aren't submitted as form values; that's the entire point of a placeholder: it displays in the input, but it is not treated as a value.
You'll need to use the regular value="<?php echo $pid; ?>" to submit the value with the form.
<form method="post">
<input type="text" name="example">
<input type="submit" value="Post me!">
</form>
Ensure you're declaring the POST and if you want to a redirect the POST to another file for handling use action.
You're picking it up correctly however,
$_POST['example']; in this case.
This would POST the input to the same file so ensure the handler is in the same context. If you do not declare post it will assume its a get request.
If you want to catch the POST in the same context then what you're doing is right. If you want to POST it to another PHP file which handles it then you cannot catch it in the same context.
Hope that helped.
Instead of placeholder use value attribute
<input type="text" id="ptid" name="ptid" readonly="readonly" value="<?php if(isset($pid)) echo $pid; ?>">
You will get value on next page.
Because value is submitted not placeholder.
I think that it's possible only with javascript - in HTML document you must create new hidden input and insert there a value with
$("#ptid").attr("placeholder")
I would like to understand why the below is not working. It is code that I have inherited and it did work on the previous webmaster's hosting.
The page has a $service variable in the URL hence the echo $_GET['service'] below in order to display the value of the variable on the page. However, we want to use that same variable value on the next page also.
At the moment I cannot use echo $_POST or I even tried echo $_GET on the next page to display this
value. There must be something out of date or wrong with <input type="hidden" name="service" value="<? echo $service; ?>" />.
I tried value= echo $_GET['service']; but this did not appear to change anything,
Grateful for all help.
Thanks.
<?php echo $_GET['service']; ?>
<form action="send-order.php" method="post">
Email<br /><input name="email" value="<?echo $email;?>" type="text" style="width: 350px;" />
<input type="hidden" name="service" value="<? echo $service; ?>" />
<input type="submit" value="Order now" /></p>
</form>
Uhm, first of all I'm seeing your form action is POST (instead of GET), so it's natural you don't appen the service input to the url...
Another thing, you're saying that the code worked previously: it might be for the use of register_globals (turned ON; in darker times that was the usual setting, now it's disabled by default) on the previous server's settings, which automatically made available to the var $service what should have been called with $_GET['service'] (or $_POST['service'], for what that matters).
I still don't understand where the "p" param comes in the URL from your comment, though. If you change the form action to action="get", you will have something like "email=something&service=somethig", but 'p' ?
$service is a variable.
http://example.com?service=foo is a GET parameter.
$service is not the same as $_GET['service']
$_POST['service'] is not the same as $_GET['service']
That said, the form's method is "post". If you're posting to the same page where the form is located, then use $_POST['service'] instead. Or change the form method to "get" and use $_GET['service'].
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).
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.