Is there anyway to save a textboxs as an array similar to how multiple checkboxs can be saved as an array? When you save multiple checkboxs to an array the value only comes up in the array when the checkbox has a check in it. Is this the same with textboxs? Do they only have value when they're not empty?
I'm trying to save these textboxs as an array (I only took the part of the form that you all need to see)
<form action="formemail.php" method="POST" name="rcr">
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="text" name="desc[]" id="desc"></td>
<td><input type="submit" name="sendwork" id="sendwork" value="Send Work Order"></td>
</form>
In the next page I'm trying to take these values and just output them in a simple way (later I'm going to be adding them to an email using foreach if I can)..... below is formemail.php
<?php
$description = $_POST["desc"];
echo $description[0];
echo $description[1];
?>
I can't get anything to echo for the life of me. And I'm not sure what I'm doing wrong.
tried ?
if( isset( $_POST["desc[]"] ) ) echo "is set ; )";
btw. same multiple id's can cause you problems if you work with JS/JQuery.
Try to use foreach and test submitted values:
foreach($_POST['desc'] as $post)
{
echo !empty($post) ? $post.'<br />' : 'An empty value.<br />';
}
Related
This is the code for the admin panel of a certain site. Appointments asked by customers will be shown in this page. The admin will be able to change the appointments based on availability.
<?php
while($row = mysqli_fetch_assoc($result)){
?>
<form action="adminEdit.php" method="POST">
<tr>
<td><input type="text" id="name" value="<?php echo $row['Name'];?>"></input></td>
<td><input type="text" id="address" value="<?php echo $row['Address'];?>"></input></td>
<td><input type="text" id="phone" value="<?php echo $row['Phone'];?>"></input></td>
<td><input type="text" id="license" value="<?php echo $row['Car_License_No'];?>"></input></td>
<td><input type="text" id="engine" value="<?php echo $row['Car_Engine_No'];?>"></input></td>
<td><input type="text" id="date" value="<?php echo $row['Date'];?>"></input></td>
<td><input type="text" id="mechanic" value="<?php echo $row['Mechanic'];?>"></input></td>
<td><input type="submit" name="submit" value="Change"/></td>
</tr>
</form>
<?php
}
?>
Here, each row of data has a corresponding button which will be used for changing or modifying the records of that particular row. Once the admin changes a specific appointment it should get updated in database.
My problem is, all the rows are getting generated by a while loop. Now how can I access a specific row when the change button of that specific row has been clicked ? As the rows are getting generated by a loop, I wont be able to access them bynameoridbecause all of them will have the samenameandid`.
Searched for relevant questions but none of them matched with my scenario. It will be of great help getting an answer. Thanks in advance.
Personally I'd be inclined to take the output from being in the loop for better control over the data and resolving the issue. You're also creating a new form on each loop.
Just loop the DB and create a new variable, then use that variable to output the data in the form.
Example code to show the basic idea, not tested or stating it's complete etc:
while ($row = mysqli_fetch_assoc($result)) {
$someNewVar[$row['id']] = $row;
// The 'id' index would be from DB which identifies individual rows
}
?>
<form action="adminEdit.php" method="POST">
<?php
foreach ($someNewVar as $index => $value) {
?>
<tr>
<td>
<input
type="text"
id="<?php echo $index;?>"
value="<?php echo $value['Name'];?>">
</input>
</td>
<td>
<input
type="submit"
name="submit"
value="Change"/>
</td>
</tr>
<?php
}
?>
</form>
Then you'd need to have the row ID passed from clicking the submit button.
On a side note, this whole approach could be tidied up, and data should be obtained in one file separate to where you output it.
Then in the file which obtains the data you can set the array to something which is also identified in the output file to manage if no data was obtained.
ie
getData.php
while ($row = mysqli_fetch_assoc($result)) {
$dataFromDb[$row['id']] = $row;
}
$someNewVar = !empty($dataFromDb) ? $dataFromDb : false;
showData.php
if ($someNewVar) {
// do the loop and form
} else {
echo 'sorry no data found';
}
I'm a beginner in php and I have a problem regarding checkbox.
First thing is that, in the form I don't know if name=medID[] is specifically used as an array. or a normal string like medID will work? and how exactly is it useful to use an array.?
When I'm updating the value in query both $quan and $medID values are not passing in the query.
In browser it shows "Alloted Succesfully" but the database value of quantity is not changing. when i replace $quan and $imp value to some integers then it works fine.
<tbody>
<tr>
<form method="post" action="ytube.php?array=hospitalstock&hospitalID=<?php echo $opened['hospitalID']; ?>&id=allot" role="form">
<div class="form-group">
<td class="vcenter"><input type="checkbox" name="medID[]" id="check" value="<?php echo $list['medID']; ?>" /></td>
</div>
<td><?php echo $list['item'] ?> </td>
<td><?php echo $list['price'] ?> </td>
<td><?php echo $list['quantity'] ?> </td>
<td><?php echo $list['subtotal'] ?> </td>
<div class="form-group">
<td><input type="text" name="quantity" id="quantity" class="form-control" /> </td>
</div>
</tr>
<?php }} ?>
<div class="form-group">
<input class="submit" type="submit" value="Allot Medicine" name="submit" class="form-control" />
</div>
</form>
</tbody>
</table>
<?php
$id=$_POST['medID'];
$quan=$_POST['quantity'];
if(isset($_POST['submit'])){
if(empty($id) || $id==0){
echo 'Select medicines to allot ';
}else{
echo $quan;
$imp= implode(", ",$id);
$q="UPDATE hospitalstock SET quantity= (quantity - '.$quan.') WHERE medID IN('.$imp.')" ;
$r=mysqli_query($conn, $q);
if(isset($r)){
echo 'Alloted Succesfully';
}
}
}
?>
Yes a normal string like madID will work just aswell.
name="medID" => $_post['medID']
name="medID[]" => $_post['medID'][0] de last [0] will get you the first element of the array
An array could be really helpful when it's a dynamically created form. A form where the number of inputs is not set, for example a contact form where you can click on a plus icon to add another text input for multiple phone numbers. Bacause its unknown how many phone numbers someone have its easier to just retrieve one variable as an array and iterate over this array after.
Don't you get an error like:
Notice: Undefined index: medID in ....
In your code you have name="medID[]" and $_post['medID']. So your form is sending an array but you retrive a normal variable. Just delete [] from name="medID[]"
Because of that if(empty($id) $id will always be empty so you don't even reach your query.
Few things to say; first I don't understand the concept of using a query string for action when you are using post as a method for submitting the values. Secondly if you are trying to consume the values from the query string as well; then I can't find the $_GET[] in your entire program. Third is a suggestion to use $_REQUEST[] when you are not sure about the get or post collections. Also, the name="medID[]" won't create any array for PHP. Is the ytube.php the same page where you have created this form?
SOLVED
I found he issue, while I was creating new ROW I was accidently grabbing the tag and it was creating new form wrap every time I added the row.
So it ended up being:
form
Row 1
form 2
Row 2
form 3
Row 3
form END
I am trying to submit my form with dynamically added fields however I keep only getting the initial one.
SetUp
HTMLDOM
FORM<br/>
div->Item<br/>
--Input<br/>
--SelectOption<br/>
--TextArea<br/>
--RadioButton<br/>
div->Item(EndWrap)
I have jQuery Grab initial wrap into a variable, once I click ADD ROW elements get +1 count.
I have tried the following on form Submit:
foreach ($_POST['user'] as $key=>$value ) {
echo $key . " : " . $value;
}
<input id="user" type="text" name="user[]" autocomplete="off"/>
However that only shows me the user of the first ITEM -> Wrap or ROW.
My question would be what is the best way to iterate over dynamically added elements inside the form on submission so that I can add values to the DB.
Thank you!
Even a direction to a valid tutorial will be greatly appreciated (had no luck finding any that work).
Match your code with the following one:
<?php
if(isset($_POST['sub'])){
foreach ($_POST['user'] as $key=>$value ) {
echo $key . " : " . $value."<br />";
}
}
?>
<html>
<body>
<form method='POST'>
Val 1: <input id="user" type="text" name="user[]" autocomplete="off"/><br />
Val 2: <input id="user" type="text" name="user[]" autocomplete="off"/><br />
Val 3: <input id="user" type="text" name="user[]" autocomplete="off"/><br />
Val 4: <input id="user" type="text" name="user[]" autocomplete="off"/>
<input type="submit" name="sub"/>
</form>
</body>
</html>
This is working for me. Although, the ids must not be same for all fields but I am taking same as per your case.
Form:
<form method="POST" action="edit_work.php">
<input type="hidden" name="wid[]" size="1" value="<?php echo "$wid1" ?>" >
<input type="text" name="course[]" size="15" value="<?php echo "$course1" ?>" >
PHP:
extract($_POST);
for($i=0;$i<$count;$i++) {
echo $wid[$i];
echo $course[$i];
}
gives the wid values OK but not the text entered for the course names.
I have been through all forums for 2 days now. Any help? Thanks.
If you want your PHP to retrieve your data from the form, can't you name your text input "course", then get it inside your PHP with $_POST['course'] ?
What is your $count ?
Using brackets with your name attribute inside your input tag may be dangerous.
If you're using a list of inputs maybe you can define a text format like name="course#" where # is your index and then access it form your $_POST variable using $_POST['course'.$index]
You don't need to extract($_POST) in that case.
Dunno if the title makes sense, but I have a variable which would to put it in basic terms would be called like this:
$_POST['something'+$variable2]
I have a form which is for editing selected records, this form contains entries for all previously selected records:
<form name="input" action="editcar.php" method="POST">
<input type="submit" value="Yes">
while($row = mysqli_fetch_assoc($result))
{
echo'
</div>
<table style="color:white">
<tr>
<td style="text-align:right">Manufacture:</td><td><input type="text" name="manufacture'.$row['carIndex'].'" value="'.$row['make'].'"></td>
<td style="text-align:right">Model: </td><td><input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'"></td>
</tr>
<tr>
<td style="text-align:right">Colour: </td><td><input type="text" name="colour'.$row['carIndex'].'" value="'.$row['colour'].'"></td>
<td style="text-align:right">Reg: </td><td><input type="text" name="reg'.$row['carIndex'].'" value="'.$row['Reg'].'"></td>
</tr>
<tr>
<td style="text-align:right">Price: </td><td><input type="text" name="price'.$row['carIndex'].'" value="'.$row['price'].'"></td>
<td style="text-align:right">Mileage: </td><td><input type="text" name="mileage'.$row['carIndex'].'" value="'.$row['miles'].'"></td>
</tr>
<tr>
<td style="text-align:right">Max MPH: </td><td><input type="text" name="mph'.$row['carIndex'].'" value="'.$row['mph'].'"></td>
<td style="text-align:right">MPG: </td><td><input type="text" name="mpg'.$row['carIndex'].'" value="'.$row['mpg'].'"></td>
</tr>
</table>
</form>
</div> ';
}
?>
</form>
The form is looped for each record previously chosen, to enable mass editing. The isue arouses when I realised I'd have multiple inputs with the same name, so I did:
<input type="text" name="model'.$row['carIndex'].'" value="'.$row['model'].'">
Placing the primary key of the record it was currently tired to on the end of it's name. Which seemed like a logical way to go about things.
However now I need to call these variables to place in the mysql query and I dunno how to do that, or even if I can.
I have the selected records saved in an array so I have:
foreach ($postid as $carID)
{
$query = "stuff";
mysqli_query($db, $query);
}
Each loop has $carID containing the variables that was put on the end of the form input names.
So something like:
$_POST['something'+$variable2]
is all I can think of but doesn't work.
Any method that works for my overall code is welcome not just a solution to the issue I've made.
Actually your way should work. Just replace the + with . in $_POST['something'+$variable2].
My tip is: use an array as name in your html instead:
<input type="text" name="model[]" value="'.$row['model'].'">
On php-Side you can loop through all $_POST['model'] since its an array now.
You can add the index for every entry in your html, too:
<input type="text" name="model['.$row['carIndex'].']" value="'.$row['model'].'">
PHP uses a dot for concatenation, not + like Java and Javascript:
$_POST['something' . $variable2]
Try something like this:
<form ...>
<?php
while($row = mysqli_fetch_assoc(...):
$index = $row['carIndex'];
?>
<input type="text" name="carmodel[<?php echo $index?>][model]" value="<?php echo $row['model'] ?>">
<?php endforeach; ?>
</form>
This way you will have the data stored in $_POST['carmodel'] as an array indexed by carIndex value as the structure of data in $_POST is defined by names of inputs, here you will have names likee carmodel[1][model] for example so then in post it will be in $_POST['carmodel'][1][model]
you can read here as well
How would I create this array structure in an HTML form?