Passing value with a button in the form - php

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)

Related

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>

Button functioning as link does not work with $_GET

I am using a button to function in the same way as a hyperlink:
<form action="intro.html"><input type="submit" value="CLICK HERE TO ENTER" ></form>
This works for static links, but does not work with PHP $_GET arguments.
<form action="wrong_choice.php?stage=0"><input type="submit" value="Wrong Choice!" ></form>
Clicking that will proceed to "wrong_choice.php" but not "wrong_choice.php?stage=0"
How can I fix that?
Thank you
Better to use:
<input type="button" value="Wrong Choice!" onClick="document.location.href('wrong_choice.php?stage=0');" />
If you do not want javascript, add method to form, delete parameter from action and add input with type hidden, which stands for parameter.
Action does not accept query string!
If you want to append data into the form which isn't part of the inputs filled by the user, add inside the <form>
<input type="hidden" name="stage" value="0" />
Action is what you want to do with the information in the form: you want to send the form in a email or send the information to another script to manage or comeback to same script.
If you want pass arguments in the form you should put them in form's fields like that:
<form action="wrong_choice.php>
<input type='hidden' value='0' name="stage">
<input type="submit" value="Wrong Choice!" >
</form>
Thanks

add query on href using input

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

Getting correct URL when using GET

When clicking a button, I would like my URL to become this:
users.php?action=search&formvar1=value&formvar2=value&...
This is what I've tried:
<form id="search" action="users.php?action=search" method="get">
But this doesn't seem to work (it doesn't add the action=search part). Is there any way to do this? I know it works when using POST instead of GET, so why wouldn't it here?
Thank you
You can do that similarly to a POST form. Simply include the default attribute as hidden form field:
<form id="search" action="users.php" method="get">
<input type="hidden" name="action" value="search">
This way it will be added as parameter to the URL like all other variables.
The browser is building the query string from scratch.
Instead, you can add an <input type="hidden" name="action" value="search" />.

How can I convert my button into a hyperlink in PHP?

I need to replace a button on my web page with a hyperlink.
I am calling a PHP script using the button.
I extract the id using the following statement:
$id = $_POST['id'];
HTML code:
<form id="test1" method="post" action="my.php?action=show">
<input type="hidden" name="id" id="id" value="1" />
<input type="submit" name="submit" value="Click" onclick="return display(1);" />
</form>
Here is what I came up with:
Click
Does my code have a flaw? Is there a better approach?
Looks good, except for three things:
Use & instead of &.
Use id=1 instead of id='1'.
Use $_GET instead of $_POST. If you want backwards compatibility, you can opt for $_REQUEST.
You can make the link post the form:
Click
That way it works without changing the PHP code.
No - looks fine to me, though the '1' doesn't need to be in inverted commas, andy you'll need to change your $_GET to $_POST in your first line of PHP.

Categories