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.
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>
How can I update a url variable value?
The url will look like that for example:
www.example.com/product.php?.........&page=1...........
I would like to change page value from 1 to 2, the reason that I typed dots instead of real url is since the url is dynamic and not static.
So how can I do that?how can I update and url variable value?
EDIT
I will refresh the page with the new value so the page will be re-filtered.
Can you try this,
$QUERY_STRING = $_SERVER['QUERY_STRING'];
echo $QUERY_STRING = str_replace('page=1', 'page=2', $QUERY_STRING);
PHP does not control your browser. The user and/or Javascript does. This means, you can not update a specific variable in the URL. (This variable is also called a HTTP GET Parameter).
What you have are below options:
Use an HTML form, with action="" & method=GET. Use hidden fields, with name=page and value=<number>.
<form action="" method="GET">
<input type="hidden" name="page" value="2" />
<!-- Use PHP to echo out all other GET parameters into hidden form fields -->
<input type="submit" value="click me to go to page 2"/>
</form>
Use Javascript: Parse out the GET parameters, and modify it.
i send an HTML email with a form and a button like this:
<form action="http://myurl/index.php" method="post">
<input type="hidden" name="testing" value="123456">
<button class="btn" type="submit">SEND</button>
</form>
then in my index.php page i read the testing variable, in this way:
echo $_POST['testing'];
but i can't read the variable and give me this:
Notice: Undefined index: testing
there is a way to send a variabile from an html mail to a php page?
Oh. Got that Email Part now.
Most Mail-Programs won't do POST requests, for security/privacy reasons. Use GET here:
in HTML:
SEND
and in PHP:
echo $_GET['testing']
Of course the data is visible in that case - but that's the entire point.
Emails don't play well with a lot of fairly standard html. In this case, I'd use something like this:
Submit
And then style your anchor to look like a button. Then on your php side, use this to make sure the variable gets there:
print_r($_GET);
What happens when you replace:
<button class="btn" type="submit">SEND</button>
With
<input type="submit" value="Send" />
I realize you are probably styling it given that you have a class statement but you can still style an input type="submit" easily enough. I ran into issues posting variables using the object.
Also, FWIW, you don't need to specify the full URL in the action. In fact, you should probably do the following to safeguard your self against XSS attacks:
<form action="<? echo htmlentities('/path/to/index.php'); ?>" method="post">
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)
For my php file, I need to grab the unique form name.
The php file is executed when a user clicks the submit button. However, there are multiple submit button each with the same id, but they all have unique names. I need the name when they click on the submit button.
you dont want elements in html with the same id - bad practice in general. Your page will likely load normally but an html validator will notice it as an error.
html validator: http://validator.w3.org/
without seeing your code, its difficult to give you a definitive answer. if you have miltuple forms you can use hidden inputs. e.g.
<input type="hidden" name="form_name" />
Otherwise you can use javascript to put data in the form when the button is clicked. example javascript using jquery
html:
<form id="formid" >
<button type="button" id="someid" onclick="submitForm('btn1')" />
<button type="button" id="someid" onclick="submitForm('btn2')" />
<input type="hidden" id="btnsubmitid" value="" />
</form>
js:
function submitForm(btnID){
$("#btnsubmitid").val(btnID);
$("#formid").submit();
}
1 way is to put a hidden input inside of your form.
<input type="hidden" name="formName" value="[name of form]" />
then in your php, you can get it using
$form-name = $_POST['formName'];
pretty sure there are other ways, but this came to mind first.