text box value whilst sitting inside an array - PHP - php

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]."'>";
}

Related

Wordpress: Is it possible to have 2 forms on the same page that use $index and $value for input fields?

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.

How to separate checkbox variable results outputted in a while loop in PHP?

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

Radio button set's array created dynamically with echo in php. How to get their values?

I've created a php page which retrieves question and options(as MCQ's) from the database and in that Radio buttons are created dynamically using echo()The name for each radio button group is assigned into an array from which one by one index's value is set to the name attribute.Example:
$result=mysqli_query($db,$sql);
$numrows=mysqli_num_rows($result);//gets the number of questions
$radiogrp_name=array();
for($i=0;$i<$numrows;$i++){ //creating array of names for each set of radio buttons
$radiogrp_name[$i]="q".$i;
}
$i=0;
while(($myrow=mysqli_fetch_array($result)) && ($i<$numrows)){
echo $myrow["q_no"].". ";
echo $myrow["ques"]."<br><br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='a'/>".$myrow["A"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='b'/>".$myrow["B"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='c'/>".$myrow["C"]."<br>";
echo "<input type='radio' name='$radiogrp_name[$i]' value='d'/>".$myrow["D"]."<br><br><br>";
$i++;
}
How would I get the selected radio buttons and set them into a SESSION variable? Can anyone help with this? Or any other way to implement this?Thank you in advance!
On the page that the form is POST'ed to you are going to need to look through the $_POST array and strip out the relevant answers. Ideally you would have a list of expected indexes to drive that page, but if not you could just try to pattern match the indexes.
Something like:
$results = array();
foreach($_POST as $key => $value){
if(preg_match("/q(\d{1,3})/", $key, $matches)){
$results[$matches[1]] = $value;
}
}
results would then contain an array of values indexed by the numeral in name="q#" and you would just set it to the session or do with it whatever you need.
EDIT
by the way, you'll need to wrap your string inclusion in curlies since it is an array.
echo "<input type='radio' name='{$radiogrp_name[$i]}' value='d'/>"

PHP update is not updating database with checkbox

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.

Unsure how to get $_POST[''] value from form when the name is dynamic?

So at the moment I have the following code which is correct but I'm just unsure how I will be able to get the name=".$job_id." as it will be dynamic, applicant-jobs.php below:
<div id="container">
<?php
foreach($jobs as $job){
$job_id = $job['id'];
echo form_open('applications/applicants');
echo "<div class=\"job\">";
echo $job['name'];
echo "</div>";
echo "<input type=\"hidden\" name=".$job_id.">";
echo form_submit('submit', 'View Applicants');
echo form_close();
}
?>
Any help is greatly appreciated, many thanks. P.S. I'm using codeigniter.
You can iterate arrays by key value pairs.
foreach($_POST as $key => $val)
{
}
Read more on the docs here; http://php.net/manual/en/control-structures.foreach.php
Edit, I slightly misunderstood your question. What you're looking for is the value attribute of the <input> element:
echo "<input type=\"hidden\" name=\"job\" value=\"".$job_id."\">";
Then in PHP you just access it like:
$job_id = $_POST['job'];
By the looks of it, the name doesn't need to be dynamic at all. You're only using one per form.
The name can be static, and have the value dynamic.
ex:
echo "<input type=\"hidden\" name=\"job_id\" value=\"".$job_id."\" />";
it can be accessed via $_POST['job_id']
Don't you want to set the value rather than the name?
eg.
echo "<input type=\"hidden\" name=\"job_id\" value =\"".$job_id."\">";
Also you've got the from open and close within the loop, i think you want them outside the loop otherwise you have multiple forms and will only end up submitting one.

Categories