I got a problem & don't get the solutions, hope you can help me!
I created a form which have visitors with optional a guest.
The sense of the form is to delete the visitor and the guest.
Now you got some options:
Delete the visitor and the guest
Delete only the visitor
Delete only the guest
Here are my checkboxes in a while-loop:
while ($row = mysql_fetch_array($result_loeschen))
{
echo "<td class='disable'><input type='text' name='AnmeldungID[]' value=".$row[ID]."></td>";
echo "<input type='hidden' name='option[0]' value='0' />";
echo "<td><input type='checkbox' name='option[0]' value='1' /></td> ";
echo "<input type='hidden' name='option[1]' value='0' />";
echo "<td><input type='checkbox' name='option[1]' value='2' /></td> ";
echo "<input type='hidden' name='option[2]' value='0' />";
echo "<td><input type='checkbox' name='option[2]' value='3' /></td> ";
}
After the form is send:
foreach($_POST['AnmeldungID'] as $key => $ID) {
$option = $_POST['option'][$key];
echo $ID;
echo " = ";
echo $option;
echo "<br />";
}
But it does not work, if all are checked - the output is:
19 = 1
12 = 0
14 = 0
15 = 0
Any ideas?
Thanks!
Chris
You can just set the name to option[] in every checkbox.
Maybe radiobuttons are the better choice for your goal?
Related
I am trying to make a simple program where the user can multiply their inputs. But it always return a 0.
I already did:
$price = (int)$_POST['txtPrice'];
$quan = (int)$_POST['quantity'];
$ans = $quan * $price;
echo "$ans";
But returns 0 always.
This is the page where all the textboxes came from:
echo "<tr>";
echo "<td><input type='text' name='quantity' placeholder='How Many?'></td>";
echo "<td><input type='text' name='txtName' value='$name'></td>";
echo "<td><input type='text' name='txtPrice' value='$price'></td>";
Using your same code and just adding the form tag and a submit works for me:
<?php
if(!empty($_POST)) {
print_r($_POST);
$price = (int)$_POST['txtPrice'];
$quan = (int)$_POST['quantity'];
$ans = $quan * $price;
echo "$ans<br>";
}
echo "<tr>";
echo "<form action='index.php' method='post'>";
echo "<td><input type='text' name='quantity' placeholder='How Many?'> </td>";
echo "<td><input type='text' name='txtName' value='$name'></td>";
echo "<td><input type='text' name='txtPrice' value='$price'></td>";
echo "<input type='submit'>";
echo "</form>";
Execute it with php -S 127.0.0.1 index.php, $ans contains the value from the multiplication.
Here is the scenario. in my database, I have like 4 questions. each questions have individual 5 radio buttons. I tried to retrieve the information from database. it shows my list of questions and radio buttons individually, but the BIG problem here is they are in the same group. example. in question 1, I picked 1st radio button, then in question 2, I picked the 2nd radio button. in question 1, the radio button I choose disappeared. so basically. the whole loop have single radio button name. how do I fix this dynamically? like auto change of radio-button-name for each question?
<form method='post' action='test.php'>
<?php
include 'db_connect.php';
$query = "SELECT * FROM test";
$result = $conn->query($query);
$num_results = $result->num_rows;
#if ($num_results > 0) {
#}
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
echo "<input type='radio' name='question_button' value='1'>";
echo "<input type='radio' name='question_button' value='2'>";
echo "<input type='radio' name='question_button' value='3'>";
echo "<input type='radio' name='question_button' value='4'>";
echo "<input type='radio' name='question_button' value='5'>";
echo "<br>";
}
$testbutton = isset($_POST['question_button']) ? $_POST['question_button'] : "";
if (isset($_POST['submit'])) {
echo $testbutton;
}
?>
<html>
<input type='submit' name='submit' value='submit'>
</form>
</html>
P.S. Edit. my original intention is to add or get the sum of the radio buttons. what syntax should i use?
to change button name dynamically
change like following
$question=0;
while ($row = $result->fetch_assoc()) {
extract($row);
echo $row['test1'];
$question++;
echo "<input type='radio' name=".$question." value='1'>";
echo "<input type='radio' name=".$question." value='2'>";
echo "<input type='radio' name=".$question." value='3'>";
echo "<input type='radio' name=".$question." value='4'>";
echo "<input type='radio' name=".$question." value='5'>";
echo "<br>";
}
$question is just for example,
you can change redio button name according to your need.
I can't see what your database query is producing, but at a guess, I'd say you should be assigning a radio button id to your question in the database field. Then, when you get the results back, you can assign it with something like:
while ($row = $result->fetch_assoc()) {
extract($row);
echo "<input type='radio' name='".$row['question_name']."' value='".$row[question_id]."'>";
echo "<br>";
}
This should make every radio box unique so long as the data in the database is unique. If this doesn't help, may I see the results of the database query and I can edit my answer.
Simply give the radio button a group name differently as bellow example
// 1. Get the handler for counter.
$counter = 1;
// 2. Iterating through the results.
while ($row = $result->fetch_assoc()) {
$name = "question_" . $counter . "_button";
extract($row);
echo $row['test1'];
echo "<input type='radio' name=$name value='1'>";
echo "<input type='radio' name=$name value='2'>";
echo "<input type='radio' name=$name value='3'>";
echo "<input type='radio' name=$name value='4'>";
echo "<input type='radio' name=$name value='5'>";
// Increment the counter by 1 for each question.
$counter += 1;
}
I have table which is dynamically generated. I am getting all correct data. I want to store html data to database table. So how do I store it? following is the code for html table
foreach($res->result() as $row ){
echo "<tr>";
echo "<td><input type='hidden' style='width:80%;' value='".$row->product_id."'
name='product_id[]'/></td>";
echo "<td><input type='hidden' style='width:80%;' value='".$product_name."'
name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' style='width:30%;' id='packing' name='packing[]'/></td>";
echo "<td><input type='text' class='quantity' style='width:80%;' readonly=''
value='".$row->quantity."' name='quantity[]'/></td>";
echo "<td><input type='text' name='rate' style='width:80%;' class='rate'
name='rate[]'/></td>";
echo "<td><input type='text' style='width:100%;' class='amount' readonly=''
name='amount[]'/></td>";
echo "</tr>";
}
On form submit I have done this..
$data['cart']=array(
'product_id'=>$this->input->post('product_id'),
'product_name'=>$this->input->post('product_name'),
'packing'=>$this->input->post('packing'),
'quantity'=>$this->input->post('quantity'),
'rate'=>$this->input->post('rate'),
'amount'=>$this->input->post('amount'),
);
print_r($data);
$i=0;
foreach($data['cart'] as $row){
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$packing=$row['packing'];
$quantity=$row['quantity'];
$rate=$row['rate'];
$amount=$row['amount'];
$query = $this->db->query("insert into
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values
('$product_id','$product_name','$packing','$quantity','$rate','$amount')");
$i++;
}
But it displaying only last record in table..Anybody has any idea about this?? I want total table records to save in another table.
$product_id =$this->input->post('product_id'),
$product_name =$this->input->post('product_name'),
$packing =$this->input->post('packing'),
$quantity =$this->input->post('quantity'),
$rate =$this->input->post('rate'),
$amount =$this->input->post('amount'),
$total = count ($product_id);
for($i=0;$i<$total;$i++){
$query = $this->db->query("insert into
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values
("$product_id[$i]","$product_name[$i]","$packing[$i]","$quantity[$i]","$rate[$i]","$amount[$i]")");
}
But it will gives error when no data found for any data So please validate all fields before this code
The trick here is to use the [] notation in your HTML form elements.
So instead of this:
<input type='text' name='someVar'/>
You have:
<input type='text' name='someVar[]'/>
If you have specific keys you can do:
<input type='text' name='someVar[myKey1]'/>
In your case I would do this (HTML generation):
foreach($res->result() as $row ){
echo "<tr>";
echo "<td><input type='hidden' value='".$row->product_id."' name='product_id[]'/></td>";
echo "<td><input type='hidden' value='".$product_name."' name='product_name[]'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:40%;'>".$product_name."</td>";
echo "<td><input type='text' name='packing[]'/></td>";
echo "<td><input type='text' class='quantity' readonly='' value='".$row->quantity."' name='quantity[]'/></td>";
echo "<td><input type='text' name='rate' class='rate' name='rate[]'/></td>";
echo "<td><input type='text' class='amount' readonly='' name='amount[]'/></td>";
echo "</tr>";
}
Notice the [] after every input name? And this would be the code that responds to the form submission:
foreach ($_GET['product_id'] as $index => $product_id) {
$product_id = $_GET['product_id'][$index];
$product_name = $_GET['product_name'][$index];
$packing = $_GET['packing'][$index];
$quantity = $_GET['quantity'][$index];
$rate = $_GET['rate'][$index];
$amount = $_GET['amount'][$index];
phppos_billing_items(product_id,product_name,packing,quantity,rate,amount) values ('$product_id','$product_name','$packing','$quantity','$rate','$amount')");
}
http://php.net/manual/en/faq.html.php#faq.html.arrays
I have an array of variables that I passing to Paypal via . The problem I have is when the Paypal screen loads the items, qty, price section is empty. Here is the code snippet for the passing:- (arrays cart_stock and cart_price hold the data to pass, $tmp_cart_count hold the total number of items to pass)
<?php
echo "<form target='PayPal' action='https://www.sandbox.paypal.com/cgi-bin/webscr' method='post'>";
echo "<input name='cmd' value='_xclick' type='hidden' />";
echo "<input name='upload' value='1' type='hidden' />";
echo "<input name='business' value='sales#danskvintage.com.au' type='hidden' />";
// other irrelevant variables are passed here //
$i=0;$count=1;
while ($i < $tmp_cart_count)
{
echo "<input name='custom' value='".$_SESSION['cart_stock'][$i]."' type='hidden' >";
echo "<input name='item_name_".($count)."' value='".$_SESSION['cart_title'][$i]."' type='hidden' >";
echo "<input name='quantity_".($count)."' value='1' type='hidden' >";
echo "<input name='amount_".($count)."' value='".$_SESSION['cart_price'][$i]."' type='hidden' >";
echo "<input name='shipping_".($count)."' value='0' type='hidden' >";
$i++;
$count++;
}
echo "<input type='submit' value='PayPal'>";
echo "</form>";
?>
Any help would be much appreciated, as it has me stumped.
Try changing
echo "<input name='cmd' value='_xclick' type='hidden' />";
into
echo "<input name='cmd' value='_cart' type='hidden' />";
I believe that might help.
Below are my static check boxes with values.
What I need is when user Checks any of the check boxes and then click submit I need the checked chec kboxes values in php further i want to place them in mysql query like this
select * from table where "all check boxes values"
My search in mysql will be based on all the checked values
echo "<table border='0'>";
echo "<p>Make a Selection to view details</p>";
echo "<tr><th>Scheme Type</th><th>Scehme Sector</th><th>Area</th><th>Year</th></tr>";
echo "<tr><td><input type='checkbox' name='chkSkills' value='ADP' /> District ADP <br/>";
echo "<input type='checkbox' name='chkSkills' value='cmd'/> CM Directives <br/>";
echo "<input type='checkbox' name='chkSkills' value='tkpp'/> Tameer KPK Program <br/>";
echo "<input type='checkbox' name='chkSkills' value='tbcess' /> Tobacco Cess</td>";
echo "<td><input type='checkbox' name='chkSkills' value='wss' /> Water Supply & Sanitation <br/>";
echo "<input type='checkbox' name='chkSkills' value='road'/> Roads <br/>";
echo "<input type='checkbox' name='chkSkills' value='ccb'/> CCB <br/>";
echo "<input type='checkbox' name='chkSkills' value='health'/> Health <br/>";
echo "<input type='checkbox' name='chkSkills' value='education'/> Education <br/>";
echo "<input type='checkbox' name='chkSkills' value='others' /> Others</td>";
echo "<td><input type='checkbox' name='chkSkills' value='pk77'/> PK-77 <br/>";
echo "<input type='checkbox' name='chkSkills' value='pk78'/> PK-78 <br/>";
echo "<input type='checkbox' name='chkSkills' value='pk79' /> PK-79 </td>";
echo "<td><input type='checkbox' name='chkSkills' value='1112'/> 2011-12 <br/>";
echo "<input type='checkbox' name='chkSkills' value='1011'/> 2010-11 </td></tr>";
echo "<tr><td colspan='4' align='right' style='background-color:white;'><input type='submit' name='devsub' value='Submit' /></td></tr>";
echo "</table>";
Regards
change name = chkSkills[] for each checkbox
and retrieve via $_POST['chkSkills']
Than just do this
foreach($_POST['chkSkills'] as $key => $value){
MYSQL_QUERY
}
use name chkSkills[] and then
use in php
if(count($_POST['chkSkills']) > 0)
{
$checkboxes=implode(',',$_POST['chkSkills']);
}
then
make query
select * from table where cloumnname in($checkboxes)