Submit buttons: button text different from value - php

I am writing a search form in PHP.
I want the user to be able to add search fields using following submit button:
<input type="submit" name="fields" value="<?php echo $fields+1 ?>" />
Now the button shows the value of $fields + 1.
Actually what I want is the button to show something else (like add new field).
Just adding text between the <input>...</input> tags does not help.
The text just appears right of the button.
How do I change the text on the button and still pass the value of $fields + 1 to GET/POST?

You can use an hidden input to store the value count, it will be available after GET/POST:
<input type="submit" name="fields" value="add new field" />
<input type="hidden" name="fieldsCount" value="<?php echo $fields+1 ?>" />

You can use the button element:
<button type="submit" name="seven" value="7">Push Me</button>
Refs: http://www.w3schools.com/tags/att_button_type.asp

You Can use the hidden field to store or post the $fields+1 value:
<input type="hidden" name="fields" value="<?php echo $fields+1 ?>" />
<input type="submit" name="submitter" value="Send" />

Related

fetching value with the submit button in HTML?

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']);
}
?>

PHP - Retrieve via $_POST the hidden input's value between several others

I have a table that is in a foreach loop, and every iteration the hidden input receives the ID from the database. My table has in each column an input field because I'd like to edit the data from the database right in the table, and to make it happen I had to put a submit button that when clicked retrieves the ID from the hidden input into another file that then does the operation.
<form action="../php/Operations.php" method="post">
//...
<?php foreach ($Service->select() as $col) { ?>
<input type="hidden" value="<?php echo $col["id"]; ?>" name="id"/>
//...
The problem is that the $_POST value is always the very first from the table, because there are n hidden input with the same name.
I'd like to know if there is a way to retrieve the hidden input's value from the clicked row, having in mind I'm not using $_GET, but a submit button,
<button type="submit" name="submit" value="update" class="btn_action">Editar</button>
that when clicked is supose to execute the code I wrote:
switch($_REQUEST["submit"]) {
case "update":
$ServiceDatabase->update($_POST["id"]);
break;
//...
Thank you.
If you want multiple inputs with the same name use name="id[]" for the input name attribute. $_POST will then contain an array for name with all values from the input elements. Then you can then loop over this array.
Example:
<form method="post">
<input type="hidden" name="id[]" value="foo"/>
<input type="hidden" name="id[]" value="bar"/>
<input type="hidden" name="id[]" value="baz"/>
<input type="submit" />
</form>
PHP
$ids= $_POST['id'];
print_r($ids);
EDIT: You can change your code like this so each submit button is tied to exactly one hidden input with the name="id".
<?php foreach ($Service->select() as $col) { ?>
<form action="../php/Operations.php" method="post">
<input type="hidden" value="<?php echo $col["id"]; ?>" name="id"/>
<button type="submit" name="submit" value="update" class="btn_action">Editar</button>
</form>
<?php } ?>
Rendered HTML:
<form action="../php/Operations.php" method="post"><input type="hidden" value="1234" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
<form action="../php/Operations.php" method="post"><input type="hidden" value="5678" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
<form action="../php/Operations.php" method="post"><input type="hidden" value="9101112" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
i would recommend you to use ajax for submitting the data
Just create a click event in for the button and get the button id
in your htm form create an id attribute like this
<input type="hidden" id="colId<?php echo $col["id"]; ?>" value="<?php echo $col["id"]; ?>" />
and your button should look like this
<button class="buttonSubmit" id="<?php echo $col["id"]; ?>" >Submit</button>
$(".buttonSubmit").click(function(){
var id = $(this).attr("id");
var hiddeninput = $("colId"+id).val();
// here you can process how would you send the data to a php file
});
Either wrap each col in a separate form and have a separate submit button per col or make the name attribute an array. i.e. name="id[<?php echo htmlspecialchars($id); ?>"

A submit button to send data from one php function to another

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" />

add the value of javascript in the hidden field

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;

multiple submit button

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.

Categories