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?
Related
Many thanks for checking this question. I have a simple email system on my website between users. Below is a section of code which is a foreach loop pulling out each email in a users inbox and displaying the subject and author etc. I have a delete checkbox to go with each one. I am struggling to see how I get catch the emails that have been selected for deletion in a POST request. I am thinking that it probably involves an array but not sure. All checked boxes have the same value of 'delete' so it should be easy enough, but I can't find a solution
<!-- LOOPING THROUGH ALL THE RESPONSES TO THE GIVEN THREAD -->
<?php foreach($messages as $message):?>
< div class="message_strip">
<table>
<tr>
<td>
<?php $id = $message->sender_id;?>
<img style="width:30px; margin:2px;"
src="../<?php echo grab_thread_thumbnail($message->sender_id); ?>"/>
</td>
<td>
<div>
<?php echo $message->style_email($message->message_id);?>
<div class="sender">
<?php $user=User::find_by_id($message->sender_id);
echo htmlentities($user->first_name);?> wrote on
<?php echo datetime_to_text($message->time); ?>
</div>
</td>
<form action="message_folder.php" method="post">
<td class="delete_checkbox">
<input type="checkbox" class="cb-element" name="delete" value="delete">
</td>
</form>
</tr>
</table>
</div>
</a>
<?php endforeach; ?>
Don't wrap the checkbox input in the form tags. Wrap your entire foreach in the form tags, and you'll need a submit button.
Assuming you want the message_id:
<input type="checkbox" class="cb-element" name="delete[]" value="<?=$message->message_id ?>">
Then you will have an array of message_ids that were checked in $_POST['delete'].
You need to give each checkbox a unique name, e.g. name='delete[$message->sender_id]', then iterate over the POST'ed array.
So i've created an administration page that creates X-number of forms based on how many users we have in our database, each of which have a submit button next to them to submit changes to the dates we have in our DBs. My problem is that when I want to get the value of what gets posted I can't extract exactly what I need from what gets posted. It gets saved into an array called and when I print_r the array I get exactly what I want, which is:
[1] => "whatever date they typed in"
(obviously the 1 changes depending on which item they changed the date of)
I need be able to query my datebase by:
UPDATE users SET subdate="whatever they typed in" WHERE id="the array reference number"
I know exactly what I need to do, I'm just not as familiar with SQL as i'd like to be, so any help would be greatly appreciated. Thanks in advance.
Code for reference:
<div class="form-section grid12" id="changedates">
<h1>Change Dates</h1>
<?php
$query = mysql_query("SELECT * FROM users WHERE admin='y'");
?>
<table>
<?php
while($row = mysql_fetch_assoc($query)) {
?>
<tr>
<td>
<h5><?php echo $row['displayname'];?></h5>
</td>
<td>
<form action="" method="POST">
<input type="text" name="subdate[<? echo $row['id'] ?>]" value="<?php echo $row['submissiondate'];?>">
<input type="text" name="nextupdate[<? echo $row['id'] ?>]" value="<?php echo $row['nextupdate'];?>">
</td>
<td>
<input type="submit" value="Set Date" name="setdate">
</form>
</td>
<?php
}
?>
</table>
</div>
You could use foreach...
foreach ($_POST[nextupdate] as $rowId => $time)
{
// do db update
}
Edit: Just realised you have more than one input per form.
Why not name each input with an array name:
<input type="text" name="form_data[<?= $row_id ?>][subdate]">
<input type="text" name="form_data[<?= $row_id ?>][nextupdate]">
In PHP:
foreach ($_POST[form_data] as $rowId => $values)
{
$subdate = $values[subdate];
$nextupdate = $values[nextupdate];
// do SQL stuff
}
I've been trying to develop a real estate page where people can add listings. I am new to the world of php mysql. I have been over this problem for over a day and can't figure out where the problem is.
I have a form where people can add data. That's good and working. Now I am starting to have a place where people can add / delete / update their info. I am trying to build this step by step.
This is where a user could pull the information. My problem is with the piece of the code:
edit_form.php?idBAR=$row[id].
Full code below.
<table>
<tr>
<td align="center">EDIT DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"configS_OH.php";//database connection
$order = "SELECT * FROM braaasil_brokerstour.property";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo ("<tr><td>$row[id]</td>");
echo ("<td>$row[address]</td>");
echo ("<td>$row[day]</td>");
echo ("<td>$row[hours]</td>");
echo ("<td>Edit</td></tr>");
}
?>
</table>
</td>
</tr>
</table>
Then this tutorial try to pass id through the address bar (I don't know much about php to actually say much)
It tries to upload the data into a new form where a person could edit info.
But I can't load the data into the new form. If I use where id=7, I get the info into the form. But this method of passing the info in the address bar like ?idBAR=8... and then try to catch it in the other code (where id=$idBAR), is not working.
Here is the code:
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?php
include "configS_OH.php";//database connection
print $database;
$order = "SELECT * FROM braaasil_brokerstour.property
WHERE id='$idBAR'";
print $idBAR;
$result = mysql_query($order) or die( mysql_error() );
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="idBAR" value="<?php echo "$row[id]"?>">
<tr>
<td>Address</td>
<td>
<input type="text" name="address"
size="20" value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td>Date</td>
<td>
<input type="text" name="day" size="40"
value="<?php echo "$row[day]"?>">
</td>
</tr>
<tr>
<td>Time</td>
<td>
<input type="text" name="time" size="40"
value="<?php echo "$row[hours]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
I tried an tried and tried..
Thank you for your time in advance.
WHERE id='$idBAR'
You haven't assigned $idBAR any value. You need to read it from the $_GET array first:
$idBAR = $_GET['idBAR'];
You should, of course, check that this value exists first, and is acceptable.
I don't see anywhere you have actually used the GET data, just the reference name which is used in GET.
If you first query is working and is getting the $row['id'] value ok - you can verify this when you go to edit_form.php, in your browser URL bar at the top, does it say this:
edit_form.php?idBAR=7
(or whatever number should be there)
If so, then you just need to use the PHP GET. Your data is stored in $_GET[], and in this case, the reference name is idBAR. So your id from your previous page query is sent through the link into your URL, and on your edit_form.php page, you'd use that data as:
$_GET['idBAR']
You can use that, but personally I assign the data to a variable, such as:
$strGetId = $_GET['idBAR'];
Then you can use $strGetId throughout your code.
Also, check things like isset(), empty() etc, just so you know you are working with A) something is actually there, and B) it's not empty etc
if you are putting a variable directly in a string without concatenating, it can't be an array variable; you must concatenate those you also need to surr. so this
echo ("<td>Edit</td></tr>");
should be this
echo ("<td>Edit</td></tr>");
also, it looks like your form is sending data with POST. When you pass form data in the url string after the question mark, that is passing with get.
so...in your form where you want to use that variable, you set it up like this
$idBAR=$_GET['idBAR']; //to get the variable if it was part of the URL
$idBAR=$_POST['idBAR']; //if it was sent with post, as is the case with your form
also, request contains both get and post, so
$idBAR=$_REQUEST['idBAR'];
will work in either case.
The problem is the $row[id] is seen as text just like everything else. You want the value of $row[id]. Instead of
echo ("<td>Edit</td></tr>");
try
echo ("<td>Edit</td></tr>");
I have this code to show my table:
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<table cellspacing='0'>
<?php
if(isset($_GET["ordem"])){
if($_GET["ordem"] == 'descendente'){
echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php'>Utilizador</a></th>";
echo "<th>Email</th>";
echo "<th>Voucher</th>";
echo "<th>Categoria</th>";
echo "<th>Preço</th>";
echo "<th>Confirmação</th>";
echo "<th>Enviar mail</th>";
echo "</tr></thead>";
}
elseif($_GET["ordem"] == 'ascendente'){
echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php?ordem=descendente'>Utilizador</a></th>";
echo "<th>Email</th>";
echo "<th>Voucher</th>";
echo "<th>Categoria</th>";
echo "<th>Preço</th>";
echo "<th>Confirmação </th>";
echo "<th>Enviar mail</th>";
echo ("</tr></thead>");
}
}
else{
echo "<thead><tr><th><a title='Ordenar por título' href='visualizarVoucher.php?ordem=ascendente'>Utilizador</a></th>";
echo "<th>Email</th>";
echo "<th>Voucher</th>";
echo "<th>Categoria</th>";
echo "<th>Preço</th>";
echo "<th>Confirmação</th>";
echo "<th>Enviar mail</th>";
echo("</tr></thead>");
}
while($stmt->fetch()){
echo("<tbody>");
echo("<tr><td>$nomeUser</td>");
echo("<td>$email</td>");
echo("<td>$nomeVoucher</td>");
echo("<td>$categoria</td>");
echo("<td>$preco</td>");
echo("<td>$confirmacao</td>");
$content = file_get_contents($file,$filePDF);
echo("<td><INPUT TYPE='checkbox' NAME='mail[]' multiple='yes'></td>");
echo("</tr>");
echo("</tbody>");
}$stmt->close(); ?>
I have a checkbox in my table and I would like to know how can I get the values of each rows from the table when i selected the checkbox. I want to send email when the user selected multiple checkbox.
Thanks
It's possible that the code above is a snippet of a larger page, but if not:
You aren't actually wrapping your input elements in an HTML form tag. Doing so will cause the user agent (presumably a browser) to treat each input tag as something to be submitted to your backend form.
You should wrap the tables in a table element; tbody is a child of a table.
Regardless of the above:
It looks like your PHP code above will render the entire tbody each time the statement fetches a new row, which is a bit weird. I'd assume you only want to render a row containing mail options?
To the best of my knowledge, there is no multiple attribute allowed in a checkbox element. You may be thinking of the select element.
You are not setting a value attribute on your checkbox input tag. If the user actually submits a form, you'll either get a set of empty mail[] variables, or nothing at all, I'm not actually sure which.
Consider the code below: Note that the checkboxes have the same name attribute, but different values.
<form method="post">
<table>
<tbody>
<tr>
<td>
<input type="checkbox" name="mail[]" value="val1" id="mail-val1" />
<label for="mail-val1">Value 1</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="mail[]" value="val2" id="mail-val2" />
<label for="mail-val2">Value 2</label>
</td>
</tr>
<tr>
<td>
<input type="checkbox" name="mail[]" value="val3" id="mail-val3" />
<label for="mail-val3">Value 3</label>
</td>
</tr>
</tbody>
</table>
<input type="submit" />
</form>
Given this form, if the user selects all three of the checkboxes and submits, your server will receive a POST request with the payload:
mail[]=val1&mail[]=val2&mail[]=val3
Depending on how PHP parses that response (it's been about a decade since I've dealt with PHP), you'll probably have an array variable accessible to your application:
# mail == ["val1", "val2", "val3" ]
Hope this helps.
I think that there is either an error in my code, or my PHP or Apache is set up incorrectly.
When I submit a form with a hidden field in it, I do not get any data in my $_POST array...
When I comment out the hidden field in my code, the POST data is returned correctly...
HTML FORM
<form action='/utils/login.php ' method='POST'>
<table>
<tr>
<td colspan='2'>
Login
</td>
</tr>
<tr>
<td>
Username
</td>
<td>
<input type='text' name='userid' value='' size='12' />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<input type='password' name='password' size='12' />
</td>
</tr>
<tr>
<td>
<input type='hidden' name='formtype' value='login' />
</td>
</tr>
<tr>
<td>
<input type='submit' value='Submit' />
</td>
</tr>
</table></form>
Here is the code that is processing it in PHP...
foreach ($_POST as $var => $value) {
echo "$var = $value<br>";
}
I am using PHP 5 and Apache 2.2 on my server.
Any ideas?
EDIT...
I have narrowed it down to this...
$command = $_POST['formtype'];
When I removed the # sign from my $_POST, I am getting the following error...
Notice: Undefined variable: formtype in C:\webroot\utils\login.php on line 17
If I comment out that line, the POST data is passed into the program without a problem.
I would suggest changing the code you are using to display the contents of $_POST to a single call:
print_r($_POST);
Anytime you are displaying the entire contents of an array, this is better than a loop w/ echo, as it will show every value at every level of the array.
Also, as was mentioned in a comment, make sure you close the form in the html.
You never closed your <form> tag.
And I see now that someone beat me to it by a mile in the comments. Still, this is the right answer.
Have you tried taking the hidden input out of the table and placing it right after the opening form tag?
You can also use:
var_dump($_POST);
...to view the post variables.
Also, if any inputs are being dynamically created or might be missing from the POST variables... you can use:
variable = 'default';
if(isset($_Post['variable'])) $variable = $_POST['variable'];
...to dynamically set variables that could be there or not.
I changed my form to work with Twig. The changed form was not sending the hidden input value with post.
In case someone has the same problem, I solved it by doing the following.
The original line was:
<input hidden name='foo[{{ loop.index }}][id]' value='{{id}}' />
I sold it by making type='hidden':
<input type='hidden' name='foo[{{ loop.index }}][id]' value='{{id}}' />
Please try with:
<form action="..." method="post" enctype="application/x-www-form-urlencoded">