I am displaying list of texts with an submit button in each row called "edit".
I am using post method.
When the user want to edit more than one text they can check a checkbox and they can click on any "edit" button.And i know how handle the texts when the user checks the checkbox.
But if they want to edit only one texts then its not necessary to check checkbox.
How can i handle if the user does not check any of the check box.
For example user want to edit apartment he clicks on "edit" button which is in the same line.
How can i get the text of this in post method.
All these texts are dynamic i am getting from data base.row
<form method='post' action="edit.php">
<tr><td>
<input type="checkbox" value="1" name="check_list[]">
</td><td><input type='text' name="text[]" value="casa|house|home" /></td>
<td><input type="submit" value="edit></td>
</tr>
< tr><td>
<input type="checkbox" value="2" name="check_list[]">
</td><td><input type='text' name="text[]" value="apartments" /></td>
<td><input type="submit" value="edit></td>
</tr>
If you set the name of the Submit buttons, you'll get them in the $_POST array under that key. You can then filter the $_POST to find which button was pressed.
--- in html ---
<input type="submit" name="edit_1" value="edit>
--- in action.php ---
foreach( $_POST as $key => $value ) { // run over list of posted fields
if( strpos( $key, "edit_" ) === 0 ) { // if it starts with edit_
$valueOfPressedButton = substr( $key, 5 ); // strip away the edit_ so that only the number of the button remains
}
}
Keep in mind that is a rather strange way to handle input and will not be straight forward in code as a result.
Related
I am trying to put together a form that will allow me to create a shopping cart application for a PHP class. For the purposes of this, we are supposed to display our form information repeatedly. While I'm familiar with storing information, I do not know how to retrieve that stored information, and we haven't really gone into any detail on storing or retrieving data from the database, yet.
Currently, I have the first form, which has a list of radio buttons and items, like so:
<form action="shoppingCart.php" method="post">
<input type="hidden" name="step" value="1" />
<table id="shoppingList">
<tr class="d0">
<td class="rad"><input type="radio" name="items" value="oolongTea"></td>
<td class="item">Oolong Tea</td>
</tr>
...
This list continues on like this for 10 items. When I click the next button, it submits that data to the form, and then proceeds onto the next page. Where does that data go, I suppose is my question, and how would I go about getting it back?
I should add that I also have this block at the top of my code, for checking which form to display:
if ( isset( $_POST["step"] ) and $_POST["step"] >= 1 and $_POST["step"] <= 3 ) {
call_user_func( "processStep" . (int)$_POST["step"] );
} else {
displayStep1();
}
And this block is the block that handles the "processing" of the first form:
function processStep1() {
$_SESSION["items"] = $_POST["items"];
displayStep2();
}
The data submitted for
<input type="radio" name="items" value="oolongTea">
can be found in
$_POST["items"];
so if you selected that radio button and submitted the form, $_POST["items"] would be equal to the value attribute on that input tag ("oolongTea")
I have a list of names and some buttons with product names. When one of the buttons is clicked the information of the list is sent to a PHP script, but I can't hit the submit button to send its value. How is it done?
I boiled my code down to the following:
The sending page:
<html>
<form action="buy.php" method="post">
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<input id='submit' type='submit' name='Tea' value='Tea'>
<input id='submit' type='submit' name='Coffee' value='Coffee'>
</form>
</html>
The receiving page: buy.php
<?php
$name = $_POST['name'];
$purchase = $_POST['submit'];
//here some SQL database magic happens
?>
Everything except sending the submit button value works flawlessly.
The button names are not submit, so the php $_POST['submit'] value is not set. As in isset($_POST['submit']) evaluates to false.
<html>
<form action="" method="post">
<input type="hidden" name="action" value="submit" />
<select name="name">
<option>John</option>
<option>Henry</option>
<select>
<!--
make sure all html elements that have an ID are unique and name the buttons submit
-->
<input id="tea-submit" type="submit" name="submit" value="Tea">
<input id="coffee-submit" type="submit" name="submit" value="Coffee">
</form>
</html>
<?php
if (isset($_POST['action'])) {
echo '<br />The ' . $_POST['submit'] . ' submit button was pressed<br />';
}
?>
Use this instead:
<input id='tea-submit' type='submit' name = 'submit' value = 'Tea'>
<input id='coffee-submit' type='submit' name = 'submit' value = 'Coffee'>
The initial post mentioned buttons. You can also replace the input tags with buttons.
<button type="submit" name="product" value="Tea">Tea</button>
<button type="submit" name="product" value="Coffee">Coffee</button>
The name and value attributes are required to submit the value when the form is submitted (the id attribute is not necessary in this case). The attribute type=submit specifies that clicking on this button causes the form to be submitted.
When the server is handling the submitted form, $_POST['product'] will contain the value "Tea" or "Coffee" depending on which button was clicked.
If you want you can also require the user to confirm before submitting the form (useful when you are implementing a delete button for example).
<button type="submit" name="product" value="Tea" onclick="return confirm('Are you sure you want tea?');">Tea</button>
<button type="submit" name="product" value="Coffee" onclick="return confirm('Are you sure you want coffee?');">Coffee</button>
To start, using the same ID twice is not a good idea. ID's should be unique, if you need to style elements you should use a class to apply CSS instead.
At last, you defined the name of your submit button as Tea and Coffee, but in your PHP you are using submit as index. your index should have been $_POST['Tea'] for example. that would require you to check for it being set as it only sends one , you can do that with isset().
Buy anyway , user4035 just beat me to it , his code will "fix" this for you.
Like the others said, you probably missunderstood the idea of a unique id. All I have to add is, that I do not like the idea of using "value" as the identifying property here, as it may change over time (i.e. if you want to provide multiple languages).
<input id='submit_tea' type='submit' name = 'submit_tea' value = 'Tea' />
<input id='submit_coffee' type='submit' name = 'submit_coffee' value = 'Coffee' />
and in your php script
if( array_key_exists( 'submit_tea', $_POST ) )
{
// handle tea
}
if( array_key_exists( 'submit_coffee', $_POST ) )
{
// handle coffee
}
Additionally, you can add something like if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] ) if you want to check if data was acctually posted.
You can maintain your html as it is but use this php code
<?php
$name = $_POST['name'];
$purchase1 = $_POST['Tea'];
$purchase2 =$_POST['Coffee'];
?>
You could use something like this to give your button a value:
<?php
if (isset($_POST['submit'])) {
$aSubmitVal = array_keys($_POST['submit'])[0];
echo 'The button value is: ' . $aSubmitVal;
}
?>
<form action="/" method="post">
<input id="someId" type="submit" name="submit[SomeValue]" value="Button name">
</form>
This will give you the string "SomeValue" as a result
https://i.imgur.com/28gr7Uy.gif
In my html I have this,
<tr>
<td class="grid_cell" width="5%">
<input type="checkbox" name="workspace_trees_rpt_target" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
</td>
</tr>
this are inside a loop so what will be displayed is a lot of checkboxes with different values. And in my script I have this,
if(confirm("Delete cannot be undone, click OK button to proceed.")) {
document.forms[0].method="POST";
document.forms[0].action="delete.php";
document.forms[0].submit();
}
after selecting two of the checkboxes and click the OK button, I then go to delete.php to print_r my post data. But when the result of print_r is displayed it only showed one value for the checked checkboxes, but I was expecting two of them. How can I make it such that when I check multiple checkboxes, the post data for the checkboxes will be an array of values of the checked checkboxes?
you must set the name of the inputs as array:
name="workspace_trees_rpt_target[]"
You have given the same name to all of your checkboxes. So only one value can be returned.
Change to
<input type="checkbox" name="workspace_trees_rpt_target[]" id="<?php echo $t["tree_id"];?>" value="<?php echo $t["tree_id"];?>" />
Then in your PHP code you will get and array of $_POST['workspace_trees_rpt_target'][]
So process it like this:
if ( isset( $_POST['workspace_trees_rpt_target'] ) ) {
// at least one checkbox has been ticked
foreach ( $_POST['workspace_trees_rpt_target'] as $checkboxe ) {
// do whatever $checkbox it will be set to the value="" that you set earlier
}
}
Ok, so my problem is this:
I have a form that asks for a First Name, Last Name, and a City. The user clicks submit and then the information is displayed into this table:
<p>You entered the following data:</p>
<form action="1.php" method="post">
<input type="hidden" name="action" value="update">
<input type="hidden" name="city" value="<?php echo $user->get_city(); ?>">
<table>
<tr>
<th>First Name:</th>
<td><?php echo $user->get_fname(); ?></td>
<td><input type="button" id="edit_fname" value="Edit"></td>
<td><input type="text" id="fname" name="fname" size="20"></td>
<td><input type="hidden" id="valf" name="val" value="fname">
<input type="hidden" id="lnf" name="lname" value="<?php echo $user->get_lname(); ?>">
<input type="submit" id="subfn" value="Submit"></td>
</tr>
<tr>
<th>Last Name:</th>
<td><?php echo $user->get_lname(); ?></td>
<td><input type="button" id="edit_lname" value="Edit"></td>
<td><input type="text" id="lname" name="lname" size="20"></td>
<td><input type="hidden" id="vall" name="val" value="lname">
<input type="hidden" id="fnf" name="fname" value="<?php echo $user->get_fname(); ?>">
<input type="submit" id="subln" value="Submit"></td>
</tr>
<tr>
<th align="right">City:</th>
<td><?php echo $user->get_city(); ?></td>
<td> </td>
</tr>
</table>
</form>
So, when the user clicks the edit button a text box is displayed along with a submit button, and depending on if it's the first name edit or last name edit, I have the proper hidden values to send along with the form. The only problem is that no matter what, the hidden input 'val' is set to 'lname' even when you click the Submit button for the first name field.
Here is the jquery:
$().ready = function() {
$('#fname').hide();
$('#lname').hide();
$('#subfn').hide(); // submit button for first name
$('#subln').hide(); // submit button for last name
$('#valf').hide(); // hidden value for fname so we know to post lname
$('#vall').hide(); // hidden value for lname so we know to post fname
$('#lnf').hide(); // hidden value to post last name when first name edit submit
$('#fnf').hide(); // hidden value to post first name when last name edit submit
$("#edit_fname").click(function() {
$('#fname').show();
$('#subfn').show();
$('#valf').show();
$('#lnf').show();
if ($('#lname').show) { // if last name edit is showing, hide it
$('#lname').hide();
$('#subln').hide();
$('#vall').hide();
$('#fnf').hide();
}
});
$("#edit_lname").click(function() {
$('#lname').show();
$('#subln').show();
$('#vall').show();
$('#fnf').show();
if ($('#fname').show()) { // if first name edit is showing, hide it
$('#fname').hide();
$('#subfn').hide();
$('#valf').hide();
$('#lnf').hide();
}
});
}();
And here is the PHP for when the Submit button is clicked and changes the 'action' to 'update':
case 'update':
$val = $_POST['val'];
if ($val == 'fname') {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$city = $_POST['city'];
$user = new User($fname, $lname, $city);
break;
}
else if ($val == 'lname') {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$city = $_POST['city'];
$user = new User($fname, $lname, $city);
break;
}
I tested to see what $val is and it seems to be stuck on 'lname' no matter what. So, I think there is a problem with my jQuery code, or the problem is that the last name 'val' value is being sent no matter what, which it shouldn't be because I have it ID'd to be hidden when the first name button is clicked.
This is all experimental to help me learn OOP. Obviously this is not production code at all. I am currently a 1st year student towards my Associates of Applied Science Degree and have many more years until I get the bachelors in computer science. This isn't even a part of a class...I'm just bored and really enjoy programming.
So, think anyone can help? Sorry for such a long post!
Here is why. See the following two lines of code
<td><input type="hidden" id="valf" name="val" value="fname">
.....
<td><input type="hidden" id="vall" name="val" value="lname">
Those define a hidden field named val first with value fname and then with value lname. Even when you are giving them different IDs their names are still the same. And nowhere in your jquery code are you changing the value of val that is why it always remains lname.
You have to change that value depending upon your logic somewhere. You can use following jQuery selector
$("input[type='hidden'][name='val']")
Edit
So instead of this
$('#valf').show();
You can write
$("input[type='hidden'][name='val']").val("fname");
And instead of this
$('#vall').show();
You can write
$("input[type='hidden'][name='val']").val("lname");
You have two different fields with name="val" in the same form. PHP reads each of these in sequence and the second value clobbers (overwrites) the first one, leaving you with just the last value in the $_GET array element.
You can submit multiple values under the same name by specifying the variable name with [] on the end of it. This tells PHP that it's an array element, just like you would use $array[] = "new element"; in your normal code to add an element to an array.
Even using the array syntax wouldn't help you here though, since both values would be passed to the server every time you submitted the form. If you only want one section or the other to be submitted, then they need to be separate forms.
i have an html form full of text fields, checkbox's , and radio fields.
i wanted to get all the names of the fields so that i can get started in validating the information in them.
the method i am using to get them is
if(isset($_POST['submit'])) {
foreach($_POST as $name => $value) {
print $name."<br/>";
}
}
but i noticed that it only displays textbox and textarea field names and it doesnt include checkbox and radio field names through this submission. do i need to include anything for it to grab the field names of those?
Checkboxes and radio buttons work a little differently than your standard inputs. If a checkbox is present on a form that doesn't necessarily mean that it will be available in the resulting POST information. Rather, those values will only be avialable if they are actually marked (checkboxes checked and radio buttons selected). The proper way to test for their value in PHP is not to check the field value but rather to check isset() first.
For a checkbox:
$data['my_checkbox'] = isset($_POST['my_checkbox']) ? 'on' : 'off';
and for a radio button:
$data['my_radio'] = isset($_POST['my_radio']) ? $_POST['my_radio'] : false;
To be a little more descriptive let's say you have the following form:
<form action="test.php" method="post">
<input type="text" name="email" value="" />
<input type="checkbox" name="active" value="Yes" />
<input type="submit" value="Submit" />
</form>
If I were to submit that form with an email value of 'test#email.com' but not check the checkbox I would have the following in $_POST:
Array (
'email' => 'test#email.com'
)
However, if I were to submit the same form with the same email address and check the checkbox I would have the following:
Array (
'email' => 'test#email.com',
'active' => 'Yes'
)
Hope that helps.
0./ Try using the following code to see the raw posted data:
echo '<pre>';
print_r($_POST);
echo '</pre>';
1./ Make sure you use a name attribute value for your checkbox and radio inputs.
Typically for checkboxes, it will be an array.
<input type="checkbox" id"=fruit-apple" name="fruits[]" value="apple" />
<input type="checkbox" id="fruit-pear" name="fruits[]" value="pears" />
2./ Make sure they sit inside the form tag.
3./ If you submit using a javascript call, try disabling javascript and see if the error stays. If it does not, you know your javascript is the culprit.