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>
Related
This is simplified code of index.php :
<form action="index.php" method="get">
<input type="text" name="course">
<button>Find</button>
</form>
Suppose, in the text field "ADAM" is put and after pressing Find button url becomes
myweburl/index.php?course=ADAM
But I want to make it
myweburl/index.php?course=ADAM#courseid
NB: Here courseid is a div id name inside index.php. By this way, I will be able to scroll down the result area.
Do you know how to do this?
Why not do it in a more legitimate way
<form action="index.php" method="get">
<input type="text" name="course">
<input type="hidden" name="courseid" value="<?php echo $courseId;?>">
<button>Find</button>
</form>
Then you get a url like this
myweburl/index.php?course=ADAM&courseid=1234
Now you dont have to do any text manipulation in the script that processes the data you just use
$_GET['course']
$_GET['courseid']
Simply add the hashtag to the action attribute.
The browser would know to move it to the end of the url string (after the GET parameters).
<form action="index.php#courseID" method="get">
<input type="text" name="course">
<button>Find</button>
</form>
Would result:
index.php?course=xxxx#bla
Tested on Chrome, if someone find other results please update.
I am trying to pass a value through clicking a button that is inside a form.
Below is my code:
<form action=deleteProduct.php?skuCode=".$row["skuCode"]." method=get><input type=submit name=delete value=Delete></form>
But from the above code I wanted to get the skuCode=".$row["skuCode"].". Instead the value passed was delete=Delete.
How should I amend my code to get the skuCode in deleteProduct.php.
Try this one :
<form action="deleteProduct.php" method="get">
<input type="hidden" value="<?php echo $row["skuCode"] ?>" name="skuCode">
<input type="submit" value="Delete">
</form>
In the deleteProduct.php page, you can access the value by using $_GET['skuCode']
It seems you may be outputting it all in a single echo. In this case, the solution will require a few extra lines, so it'd probably be best to change it to:
//close off PHP if required
?>
<form action="deleteProduct.php" method="get">
<input type="hidden" value="<?php echo $row['skuCode'] ?>" name="skuCode">
<input type="submit" value="Delete">
</form>
<?php
//resume PHP if need be
Something to that effect should work.
I'm not 100% sure how your $row["skuCode"] part works.. I guess it's PHP? Either way, just insert the value however you need to, but that HTML should do it.
Fiddle:http://jsfiddle.net/rt9yc/
In this jsfiddle, when I click on the delete button with the network tools open in my inspector I see a request for: deleteProduct.php?skuCode=testSKU ("testSKU" being the dummy SKU I was using in the fiddle)
I have an
<input type=text name=search />
and I wanted to add the value of input to my href
<a href='index.php?search=$search>submit</a>
But I think that won't work on php alone right?
How can I add the value of my input to my href as it clicks?
NOTE: need to appear in the browser url menu this way
index.php?search=anyvalue
as soon as they click it. because I'm using pagination
Straight HTML - no PHP or JavaScript Needed
<form action="index.php" method="get">
<input type="text" name="search" />
<button type="submit">Submit</button>
</form>
Once clicked it will take the user to: index.php?search={value of input}
For pagination to work it would be:
Page 2
Page 3
try this:
<form method="get" action="index.php">
<input name="search" type="text" />
<input type="submit" />
</form>
If that's not what your looking for just elaborate a bit on what you are trying to achieve.
you must open a php tag like this:
<a href='index.php?search=<?php echo $search;?>' >submit</a>
but it work after submitting the form.
use javascript
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).