to all i have a weird problem when i am trying to update a table of my database with checkboxes it takes only one value and all the rest just ingnores them here is my php code so far
foreach ($_POST['choice'] as $id){
$price=$_POST['price'][$id];
$availability=$_POST['availability'][$id];
$result1=mysql_query("UPDATE store SET storeid='".$id."',availability='".$availability."', price='".$price."' WHERE productid='".$par1."'");
}
Yes you are right and i am deeply sorry for the lack of information.Here is my html code as well
echo "<td><input type='text' id='availability[".$row->id ."]' name='availability[".$row->id ."]' value='".$row->diathesimotita ."' size='20'/></td>";
echo "<td><input type='text' id='price[".$row->id ."]' name='price[".$row->id ."]' value='".$row->price ."' size='10'/></td>";
echo "<td><input type='checkbox' id='choice[".$row->id ."]' name='choice[".$row->id ."]' value='".$row->id ."' /></td>";
echo"</tr>";
So i am trying to take the textbox values which are in the same row with the check box but it gets only the first checked ckeckbox
Checkboxes only post a value when they are checked. You should validate them or set a value first before trying to use them in a query (for security if nothing else!). For example do this at the top of your processing code:
$checkbox_val = (!empty($_POST['choice']) ? 'Yes' : 'No'); //example only assuming non-array for 'choice'
This will set the value to 'No' if the POST value is empty or not set, guaranteeing that you always have a value. You can change the values to 0/1, true/false, etc.
Also, without seeing what is in your 3 $_POST arrays, it is impossible for us to tell you if you are getting the correct values for $price etc.
The PHP code looks fine; we need to see the HTML code, but anyway try this in SQL:
UPDATE store SET storeid='".$id."' , availability='".$availability."' , price='".$price."' WHERE productid='".$par1."'" );
Spaces really matter in SQL. If this does not work please show us the HTML code.
Related
I have two checkbox forms on the same page. When they are both there, the second form will not work. I'm wondering if there is a way to have both on the same page with them both needing the $index of the input.
The input fields are similar to this (the other is unchecked):
echo "<input type='checkbox' name='check[]' value='$index' onChange='this.form.submit()' checked='checked'>";
echo "<input type='hidden' name='itemDELETE$index' value='$bookID'>";
This is the other
echo "<input type='checkbox' name='checkbox[]' value='$index' onChange='this.form.submit()'>";
echo "<input type='hidden' name='itemADD$index' value='$bookID'>";
And the data is stored like:
$value = $_POST["check"][0];
$toDELETE = $_POST["itemDELETE" . $value];
and
$value = $_POST["checkbox"][0];
$toADD = $_POST["itemADD" . $value];
I have tried renaming $value but the issue lies in the $index on the input fields.
They are outputting correctly, but the second one will not store the value properly in it's variable, whereas the first does. The second is the delete one.
When I remove the first forms functionality, the second form works. But when I add it back, the second one stops working. Not picking up on the itemDELETE $value
If I manually put the value in like this "itemDELETE0" instead of ["itemDELETE" . $value]it works. But I need the $value to picked up on since it's a foreach loop.
My solution to this was to put the second form in different shortcode, and include it on the page. It works well and doesn't clash. Same code, just in a different shortcode.
Then on the page created, I used the shortcode for the first form and then put the shortcode for the second form below it.
And instead of defaulting it to checked, I had to use it unchecked. But that works alright.
I'm trying to write an attendance system that, when a user is present at a class, the staff will check a box and the program will then add one to the present count of the relevant customers. The problem is to output the register, taken from phpMyAdmin, it uses a while loop so all the checkboxes have the same variable name. This is the code I have so far...
echo "<form 'action=badminreg.inc.php' method='post'>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row['bookingID']."</td><td>".$row['firstName']."</td><td>".$row['surname']."</td><td><input type='checkbox' name='present' value='present'</td><td><input type='checkbox' name='incident' value='incident'</td></tr>";
}
echo "<input type='submit' name='reg-submit'>";
echo "</form>";
isset($_POST['reg-submit']);
$pres = $_POST['present'];
I need to separate the check box inputs so that the program will be able to mark individual users differently to others. I'm sort of new to PHP as this is for my A-level coursework so is there a way to get around this? Any help would be great. Thanks
You can define the name for the checkbox elements as array and pass the bookingID as value.
Example (untested):
$output = "<tr><td>{$row['bookingID']}</td><td>{$row['firstName']}</td><td>{$row['surname']}</td><td><input type='checkbox' name='present[]' value='{$row['bookingID']}'</td><td><input type='checkbox' name='incident[]' value='{$row['bookingID']}'</td></tr>";
The variables $_POST['present'] and $_POST['incident'] contains then an array with the IDs of the selected check boxes.
If you change it to name='present[]' then when you submit the data again, you'll get an array of values inside $_POST["present"] instead of a single one. (You'll get one item in the array for each checkbox with that name which was actually checked - a quirk of HTML forms is that if the checkbox wasn't checked, its value is not submitted at all).
You'll also want to change the value of the checkbox to be the ID of the customer (or booking, maybe), so you can identify which box was checked.
Same for the "incident" checkbox as well, of course.
So you're aiming for something like this, I think:
echo "<tr>
<td>".$row['bookingID']."</td>
<td>".$row['firstName']."</td>
<td>".$row['surname']."</td>
<td><input type='checkbox' name='present[]' value='".$row['bookingID']."'</td>
<td><input type='checkbox' name='incident[]' value='".$row['bookingID']."'</td>
</tr>";
I've got a set of checkboxes but one shuld be always checked, I want that this checkbox is also hidden from my page, I need this beacause I have the DB that can be read only if this attribute is checked..
Please I need answers where you teach me how to hide the checkbox and NOT where you say that I can change the comand on SQL
$sqlPers= "SELECT * FROM personalizzazione";
$countPers = 0;
foreach ($dbh->query($sqlPers) as $rowPers){
$selez = '';
if($rowPers['Condimento'] == 'Base')
$selez = " checked = 'checked' ";
echo "<div class='checkbox'><label class='bianco'><input type='checkbox'" .
$selez . "name='".$rowPizza["idPizze"]."_".$rowPers["idPersonalizzazione"].
"' id='".$rowPizza["idPizze"]."_".$rowPers["idPersonalizzazione"]."'>".
$rowPers['Condimento']." (€ ".$rowPers['Prezzo'].")</label></div>";
$countPers++;
}
I tried to add type = 'hidden' in the variable $selez but nothing
Otherwise can someone say me how to disable it or hide it throught ccs?
I've tried searching on internet and there weren't full solutions for my question
There are two things you can do:
Method 1 - Hide it using CSS:
<input type='checkbox' style='display:none;'>
Method 2 - Use a hidden type instead of checkbox:
Another option would not have a checkbox, but rather have a separate hidden input tag.
<input type='hidden' name='pizza' value='value'>
You can hide it with style="display:none;"
But since it is hidden, does it have to be a checkbox?
A regular hidden input would do the job, too.
Look at the HTML that gets sent to the browser: You have already set type='checkbox' in your string, so having an additional type='hidden' may not give you what you want. You end up with <input type='checkbox' type='hidden'...
Your error was tough to see because you're writing your code in a way that is really difficult to debug. It's better practice to do all the logic up front. You're better off building all the attributes in code first. For example:
//run test to make $hide true for the loop where you want to hide the checkbox
$type = 'checkbox';
$style = $hide? "display:none" : "";
$name = $id = "$rowPizza[idPizze]_$rowPers[idPersonalizzazione]";
...
//then later when you can build your html
$html = "<div class='checkbox'><label class='bianco'><input type='$type' name='$name' id='$id' style='$style'>";
//Finally once you've built the whole html you can echo it
echo $html;
Note that we set display:none style when the $hide variable is true. Otherwise, the style will just be an empty string and the checkbox will be shown
I have a query which is doing something strange. This checkbox is in a while loop and it correctly lists out everything it needs to:
<input type='checkbox' name='rep[]' value='$invoiceID'>Reference Number: $invoiceID
<input type='hidden' name='billablehours[]' value='$billableTotal'>
When the form is submitted the values are inserted into the database using:
foreach ($_POST['rep'] as $index => $id) {
$sql2="INSERT into b_sale_basket (QUANTITY,LID,NAME)
VALUES
('".$_POST['billablehours'][$index]."','s1','".$_POST['rep'][$index]."')";
if (!mysqli_query($con,$sql2))
{
die('Error: ' . mysqli_error($con));
}
}
It inserts everything as it should do except billablehours. I have outputted the value of $billableTotal on each checkbox on the form page and the value is correct. For example it might equal 25 but when the button is pressed it inputs 37.5 which is another value of a checkbox.
Strange. Can anybody identify an issue?
Problem is that if checkbox is not checked it's not passed to server, so if u have 3 checkboxes and select first and third, on server you got them with 0, 1 index.
in your case just use specific identifiers:
<input type='checkbox' name='rep[$id]' value='$invoiceID'>Reference Number: $invoiceID
<input type='hidden' name='billablehours[$id]' value='$billableTotal'>
Assuming you know this $id is connected corresponding $invoiceID and $billableTotal,
it may be entry id from database.
And when using: $_POST['billablehours'][$index] if checkbox is not checked it gots empty...
Note: my exmplanation is just to understand the point, not 100% working example maybe, because I've got no full code what you do.
Just remember: It's the key when working with checkboxes to correctly reference the data passed from server to client and vs.
Only checked checkboxes are submitted. So if you only check one, then rep array will only have one element 0 but thebillablehours array will have as many as are defined on the page.
$i = 0;
//loop
echo "<input type='checkbox' name='rep[$i]' value='$invoiceID'>Reference Number: $invoiceID";
echo "<input type='hidden' name='billablehours[$i]' value='$billableTotal'>";
$i++;
//end loop
So the rep and billablehours indexes will match regardless of how many checkboxes were checked.
You are wide open to SQL injection attacks with your script. You either need to use prepared statements (preferable) or run your $_POST values through mysqli_real_escape_string;
Leaving my incorrect answer below, but not deleting this answer because I think the SQL injection vulnerability needs to be addressed.
That being said, your issue is that you aren't echoing the value:
value='<?php echo $billableTotal; ?>'>
Make these changes:
name="billablehours[$invoiceID]"
$_POST['billablehours'][$_POST['rep'][$index]]
I know for a fact option 1 is right as it works for me.
OPTION 1
echo "<input type='text' name='text1' id='text1' value='".$_SESSION['txt']."'>";
But in a situation where text boxes are a result of an array, if I change the above to the following is it correct? I know the name='text1[]' bit is right however can some one tell me how do I change the value attribute in the option 2?
OPTION 2
echo "<input type='text' name='text1[]' id='text1' value='".$_SESSION['txt[]']."'>";
you can put the array in to session :-
$my_array=array('ct', 'dg', 'se', 'ir');
// put the array in a session variable
$_SESSION['code']=$my_array;
foreach($_SESSION['code'] as $key=>$value)
{
// and print out the values
echo "<input type='text' name='text1[]' id='text1' value='".$_SESSION[$key]."'>";
}