If the submit button is pressed in HTML I need to send some value with it. How can I do it?
<input type="submit" name="ask" class="tbutton" value="ask" />
if(isset($_POST['ask'])){
// i need to fetch some value that is passed with submit button }
I think you want to fetch value of some input right?
may be this will help
<form method="post">
<input type="text" name="myText" />
<input type="submit" name="ask" class="tbutton" value="ask" />
</form>
<?php
if(isset($_POST['ask'])){
echo($_POST['myText']);
}
?>
Related
I want to auto submit form after 5 minutes without submit button my code is working but not get post value.Please help me.
<form name="addcontact" method="post" content="2;" action="URL=demo.php">
<input type="text" name="addontable" value="Add on Table" />
</form>
You have a wrong value for action attribute of the form - change it to:
<form name="addcontact" method="post" content="2;" action="demo.php" content="2;">
<input type="text" name="addontable" value="Add on Table" />
</form>
I'm trying to figure out how to send data from one php function to another through using a submit button. The idea is that a spellchecker will highlight the correct spelling for a word, and the user will click the button to run the program again with the correct spelling.
<form method="post" action="results.php">
<input type="submit" value="Search!" name="submit" id="searchButton"
value="<?php
$search = "dog";
$_POST[$search]
?>"
/>
This is what I have so far...
Also, is it possible for the value of a button to be a variable?
You can use hidden variables as an alternative to ajax.
<input type="hidden" value="dog" id="search" />
<input type="submit" value="Submit" />
I have this javascript line
<script type="text/javascript">document.write(top.location);</script>
and this code
<form action="rating.php" method="post">
<input type="hidden" name="url" value="xxxx" />
<input type="submit" />
</form>
<?
echo $_POST["url"];
?>
Please, How i can add the value of javascript (xxx) in the hidden field?
Also does it possible to let <input type="submit" />have automatic submit ?
Thanks
Do you mean this, in order to feed the hidden field?
<input type="hidden" name="url" value="<? echo $_POST["url"]; ?>" />
Regarding submit, you can use javascript to handle the submit event:
If you add the attribute name="FORMNAME" inside the <form ... > tag, you can use:
document.FORMNAME.submit();
for submitting the form, without using the submit button.
In order to feed a hidden field through javascript, use:
document.FORMNAME.url.value = top.location;
I have a basic form that I am using as a dummy to test some JavaScript functionality. My goal is to click the submit button and have submission paused until a javascript function is executed, then submit the form after that. the code I currently have is below, but the submission of the form is not prevented, and the hidden field's value is never set. can someone explain why this is happening?
NOTE I am aware of a jQuery method of performing the same functionality. I am specifically trying to solve this problem without using jQuery. If your only answer is "jQuery is better", please do not bother.
<html>
<head>
<script type="text/javascript">
function test(){
document.getElementById("hidden").value="hidden";
var queryString = $('#decisionform').formSerialize();
alert(queryString);
document.getElementById("form").submit();
}
</script>
</head>
<body>
<?php
if ($_POST){
foreach ($_POST as $key=>$value){
echo "<b>$key</b> $value<br>";
}
}
?>
<form action="hello.php" method="post" id="form">
<input name="first" type="text" />
<input name="second" type="text" />
<input name="third" type="text" />
<input name="hidden" type="hidden" />
<input name="submit" type="submit" value="Submit" onclick="test();return false;" />
</form>
</body>
</html>
You don't have an element with the id hidden so document.getElementById("hidden").value="hidden"; will throw an error as you can't set properties on undefined. The script will stop running and never reach return false so the form will submit as normal.
try <form action="hello.php" method="post" id="form" onsubmit="test(); return false;">
to prevent a submit, you actually need to have a return false, in the onsubmit even of the form itself
along with not having an id on the hidden field.
<input name="submit" type="submit" value="Submit" onclick="test();return false;" />
use a regular button in this case because your function is performing the submit.
<input name="submit" type="button" value="Submit" onclick="test()" />
You're missing the id attribute from your input element. Modify <input name="hidden" type="hidden" /> to <input name="hidden" type="hidden" id="hidden" />.
You can use
onclick="return test()"
and just return false at the end of the function.
Add id="hidden" to your input, like this:
<input name="hidden" type="hidden" id="hidden" />
i've a form POST with multiple submit buttons. i understand to get this to work i must have them with different name.
however, i wanna keep the name to be the same because i wanna handle the POST using a single script.
im not sure if there is other way but i know javascript can be used. however, how do i get the value of the hidden value associated to the button since now they have only a single ??
my example is as follows:
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="1" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
<input type="hidden" name="removeid" value="2" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
<input type="hidden" name="removeid" value="2" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
</form>
Your hidden values are not associated with the buttons at all. Furthermore, you cannot use the same value for the ID attribute on multiple elements.
What I usually do in this situation is check the POST vars. Name them something like remove_1, remove_2, etc. Then you can search through your POST vars, find all of them beginning with remove_ (or whatever format you choose... don't use it for other things) and then you can parse out the ID of what you are trying to remove.
You could always just use 3 different forms, all with the same action. No JavaScript needed.
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="1" />
<input type="submit" value="Remove" inputbutton/>
</form>
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="2" />
<input type="submit" value="Remove" inputbutton/>
</form>
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="2" />
<input type="submit" value="Remove" inputbutton/>
</form>
It's possible using two different methods:
If you absolutely have to show 3 different buttons, use a separate <form> wrapper for each one. Put each "removeid" element in a different form.
Otherwise, just have a single button, and when submitted, use JavaScript to set the value of a single hidden input element before posting the form. You can find sample code for this easily with a Google query for "javascript+form+post".
You can have one form with more than one submit button sharing the same name, your initial assumption was wrong.
The following code is perfectly valid, and the value of the clicked submit button will be passed along with its name:
<form action="TestZone.html" method="GET">
<input type="submit" name="MySubmit" value="First" /><input type="submit" name="MySubmit" value="Second" /><input type="submit" name="MySubmit" value="Third" />
</form>
You can't have multiple elements with same ID, but same name for form elements is common and valid.
Hi i have resolved my questions by following Brad solution. to get the POST var, i did this:
//Check if Remove btn is clicked
$isClickRemove = false;
$cid = "";
foreach($_POST as $k=>$v){
$pos = strpos($k,"btnremovecart_");
if($pos !== false){
$pos2 = strpos($k,"_"); //2nd pos to get cartID
$cid = substr($k,$pos2+1);
$isClickRemove = true;
break;
}
}
my html looks like this:
<input type="submit" id="btnremovecart_11" name="btnremovecart_11" value="Remove" />
hope this helps =)
You can't because there is no way of distingushing the different fields.