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")
Related
I have a table with a list of entries fetched by DB, each of them has a textbox, clicking a button, will do something with those textboxes. And until then, it's all good, however, when a user hits the Enter key on a textbox, the form is submitted but I can't figure out how to catch that POST request, because the 'name' value of the textbox is not fixed.
How would you do in such case?
FORM
<form action="file.php" method="POST">
<table>
<?php
while($row = $query->fetch_assoc()) {
?>
<tr>
<td><input type="text" value="<?php echo $_SESSION['arrays'][$idx]; ?>" name="array_<? echo $idx; ?>"></td>
</tr>
$idx ++;
}
</table>
<button type="submit" name="button">Submit</button>
</form>
WHERE I MANAGE THE POST REQUEST
if (isset($_POST['button']) {
//DO SOMETHING
}
If a user presses the enter key, the form is submitted but I have no idea how I can catch such request, what would be a good solution?
I tried replacing
if (isset($_POST['button']) {
with
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
but the problem persist, as I have no clue on how to get datas from the textbox, with the name not being fixed
EDIT SOLUTION
for those struggling with a dynamic field like this, I solved my issue adding an array hidden field to the table and fetching it with a POST.
<input type="hidden" name="idx[]" value="$idx">
then fetching the variable with
if (isset($_POST['idx'])) {
//this will work if user presses enter key on the dynamic textbox
}
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.
I have a form in which i have two input button one for multiple delete and another for multiple suspend items. like this-
<form>
<input type="checkbox" name="check_item[]" value="<?php echo $users['user_id']; ?>" />
<input type="button" id="suspendall" name="suspendall" value="Suspend" />
<input type="button" id="deleteall" name="deleteall" value="Delete" /></td>
</form>
when I click on delete or suspend it ask for confirm that event by jquery like-
$('#deleteall').click(function() {
if(confirm('Really Want To Delete This?')){
$('#listing').submit();
}
});
if it confirm cancel form is not submitted and if it confirm OK form is submitted and on submission i have to delete or suspend that item from db. I have write this code for this-
if(isset($_POST['deleteall'])){
$check_array = $_POST['check_item'];
$usersId = implode($check_array,',');
$db->deleteUser($usersId);
}
if(!empty($_POST['suspend'])){
$check_array = $_POST['check_item'];
$usersId = implode($check_array,',');
$db->suspendUser($usersId);
}
the problem I am facing is both times when the form is submited i got only array of ids of check boxes. I am not able to identify which button is clicked because I am not getting button value. that why its not working, and if I changed these button into submit button its working very nice but didn't ask for confirm the event. What should I do for that. Do anyone have any solution for that, Please help me. thanks
When submitting with jQuery().submit() the button on which was clicked is lost.
You could try to not submit() the form in the click handler but instead call evt.preventDefault() when ! confirmed().
In you HTML form has a hidden form field.
On the JS event set the value of the hidden field before submitting
$('#deleteall').click(function() {
if(confirm('Really Want To Delete This?'))
{
$("#hiddenFormId").val("deleteAll");
$('#listing').submit();
}
});
Then use this type of code in your PHP (Sorry not a PHP programmer)
if $_POST['hiddenFormId'] == 'deleteAll'
I'm trying to get a website working. What I have are basically two images displayed (random, taken out of a mySQL database). What I need to do is (when the user clicks one of the images) the following:
Update the page, passing the info about the selected image (submit form);
Add one piece of data to the database (upvote the image)
I need to use $_POST to pass an array of values to the next page. So I thought:
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image.png"
value ="dat1[\"data1\",\"data2\",\"data3\"]">
<!-- If value must be a single string, I'll use hidden inputs-->
</form>
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image2.png"
value ="dat2[\"data1\",\"data2\",\"data3\"]">
</form>
Then I can upvote the selected image on the mySQL database with a little php upvote() function that updates the record. The upvoting process is done when the new page is loaded. From this, I have a couple questions:
I'm guessing the images will act as buttons, right? (They are supposed to submit the form, hence refreshing the page). If not, how can I achieve this? I'm unable to do it with a link (since I can't add the values to it). Maybe a javascript function? But I don't know how to submit the form that way either...
Once the page is reloaded, does it mean that only the data from one form has been submited, so I can retrieve the data by simply calling the PHP variable $_POST['img'] and get an array back?
EDIT: I now managed to get everything working, slightly similar to what I proposed initially. Thanks for the AJAX suggestion though, since it was what helped me solve it (looked up AJAX tutorials, found solution).
Here's my solution:
<?php
echo "<form name=\"input\" action=\"F2F.php\" method=\"POST\">";
echo "<input type=\"hidden\" name =\"table\" value=\"".$table1."\">";
echo "<input type=\"image\" name=\"nom\" src=\"".$IMG_Route1."\" value =\"".$Nom_base1."\" border=\"0\">";
echo "</form>";
?>
(where the image goes)
and then, on the header:
<?php
if ($_POST['nom']||$_POST['nom_x']){
if (!$_POST['nom']){
echo 'Could not retrieve name. $_POST[\'nom_x\'] = '.$_POST['nom_x']. mysql_error();
exit;
}
if (!$_POST['table']){
echo 'Could not retrieve table. $_POST[\'table\'] = '.$_POST['table']. mysql_error();
exit;
}
upvote($_POST['table'],$_POST['nom']);
}
?>
You can use one form and a set of radio buttons to simplify things a bit. Clicking on the label will toggle the radio button. You can use commas to separate multiple values for each checkbox, which you can then abstract later on (see below)
<form name="input" action="the_page.php" method="POST">
<ul>
<li>
<label>
<img src="whatever.jpg" />
<input type="radio" name="selectedImage" id="img1" value="12,16,19" />
</label>
</li>
<li>
<label>
<img src="whatever2.jpg" />
<input type="radio" name="selectedImage" id="img2" value="12,16,19" />
</label>
</li>
</ul>
</form>
You can detect when the radio button is selected by adding a listener for the change event, then submit the form.
$('input[name="selectedImage"]').change(function() {
$('form[name="input"]').submit();
});
To abstract the multiple values, you can then explode the form result with PHP, which will return an array of the values.
$selectedImageValues = array();
$selectedImageValues = explode(",", $_POST['selectedImage']);
From there you can pull the different values out and save the data to the database.
I'll try to explain this as best as I can.
I have a form that accepts multiple fields, and in the end, e-mails all the fields to a specific e-mail address.
So for example, I have three text boxes, one list box and two submit buttons.
Two of the text boxes are first name, and e-mail address
The third text box is used to populate the list box. So if I enter, NIKE, into the third text box and push the first submit button. Nike will now be in the listbox.
I want to be able to populate the list box with as many entries as needed, then push the second submit button to send all information (first name, e-mail address and all items in list box).
The problem is, pushing the first submit button always triggers the e-mail sent, since I'm "POST"ing.
I have everything working right now. The third text box submits the new data to a table in mysql, and then retrieves all the data and puts it in the list box.
What's the best way to fix this scenario? Could I stop the Post variable from validating, until the second submit button is used?
Also, I'd like to avoid Javascript, thanks
Make sure the two submit buttons have names. I.E: <input type="submit" name="command" value="Add"> and <input type="submit" name="command" value="Send">. Then you can use PHP to determine which one was clicked:
if($_REQUEST['command'] == 'Add')
{
// Code to add the item to the list box here
}
elseif($_REQUEST['command'] == 'Send')
{
// Code to send the email here...
}
BONUS: For extra credit, make the commands variables so they can be easily changed, and map them to functions...
<?php
$commands = array(
'doSendEmail' => 'Send Email',
'doAddOption' => 'Add Option',
);
function doSendEmail()
{
// your email sending code here...
}
function doAddOption()
{
// your option adding code here...
}
function printForm()
{
global $commands;
?>
Name: <input type="text" name="name"><br>
Email: <input type="text" name="name"><br>
<input type="text" name="add">
<input type="submit" name="command" value="<?= $commands['doAddOption'] ?>">
<select>
<?php /* some code here */ ?>
</select>
<input type="submit" name="command" value="<?= $commands['doSendEmail'] ?>">
<?php
}
if(isset($_REQUEST['command']))
{
$function = array_search($_REQUEST['command'],$commands);
if($function !== -1)
call_user_func($function);
}