Multiplying $_POST - php

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.

Related

PHP MySql update query not working,says:- Undefined Index [duplicate]

This question already has answers here:
How to get input field value using PHP
(7 answers)
Closed 6 years ago.
I have follow the tutorial of it where i want to update my database using two php files.
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action =update.php method=post>";
echo "<td><input type=text name=Cname value='".$row['CustomerName']."'></td>";
echo "<td><input type=number name=size min=1 value='".$row['TableSize']."'></td>";
echo "<td><input type=date name=Adate value='".$row['DateA']."'></td>";
echo "<td><input type=time name=Atime value='".$row['TimeA']."'></td>";
echo "<td><input type=tel name=phonenumber value='".$row['PhoneNumber']."'></td>";
echo "<input type=hidden name=id value='".$row['TableID']."'>";
echo "<td><input type=submit>";
echo"</form></tr>";
}
?>
this is what i use for the first php file
as for the update.php:
<?php
$con = mysqli_connect('127.0.0.1','root','');
mysqli_select_db($con,'restaurant');
$sql = "UPDATE addtable SET CustomerName='$_POST[Cname]', TableSize='$_POST[size]', DateA='$_POST[Adate]',TimeA='$_POST[Atime]',PhoneNumber='$_POST[phonenumber]', WHERE TableID=$_POST[id]";
if(mysqli_query($con,$sql))
header("refresh:1; url=AssignBooking.php");
else
echo "Not Update";
?>
but the $sql line just doesn't work as it says that
Undefined index: Cname and other indexes too.
put quotes outside the post variable:
$sql = "UPDATE addtable SET CustomerName='".$_POST['Cname']."', TableSize='".$_POST['size']."', DateA='".$_POST['Adate']."',TimeA='".$_POST['Atime']."',PhoneNumber='".$_POST['phonenumber']."', WHERE TableID=".$_POST['id'];
According to your code put the name attributes value ' single quote.
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action =update.php method=post>";
echo "<td><input type=text name='Cname' value='".$row['CustomerName']."'></td>";
echo "<td><input type=number name='size' min=1 value='".$row['TableSize']."'></td>";
echo "<td><input type=date name='Adate' value='".$row['DateA']."'></td>";
echo "<td><input type=time name='Atime' value='".$row['TimeA']."'></td>";
echo "<td><input type=tel name='phonenumber' value='".$row['PhoneNumber']."'></td>";
echo "<input type=hidden name="id" value='".$row['TableID']."'>";
echo "<td><input type=submit>";
echo"</form></tr>";
}
?>
Put quotes accordingly
UPDATE addtable SET CustomerName='".$_POST['Cname']."',TableSize='".$_POST['size']."', DateA='".$_POST['Adate']."',TimeA='".$_POST['Atime']."',PhoneNumber='".$_POST['phonenumber']."' WHERE TableID=$_POST['id'];

PHP Basket quantity variable

I am trying to create a php page where the materials from database are populated. Users should be able to enter the quantity next to the item they wish to order and I have created a qty text field for this
<?php
session_start();
include("db.php");
$pagename="Order Material";
echo "<html>";
echo "<title>".$pagename."</title>";
echo "<h2>".$pagename."</h2>";
include ("detectlogin.php");
echo "<link rel=stylesheet type=text/css href=mystylesheet.css>";
$sql="select * from material";
$result=mysqli_query($con, $sql) or die(mysqli_error($con));
echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";
while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type=text name=qty value=qty size=5></td>";
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
echo "</tr>";
}
echo "</table>";
?>
However, I cannot successfully post the value of qty on to the next page even though I have $qty=$_POST['qty']; on my request_material.php. Do you know why this value entered in the qty field cannot be posted onto the request_material.php page? Do I need a session variable?
thanks
Because input tag name="qty" is outside the form tag
echo "<td><input type=text name=qty value=qty size=5></td>";// outside form tag
echo "<form action=request_material.php method=post>";
echo "<input type=hidden name=materialcode value=" . $arraymaterials['materialCode'] . ">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
You need to add it inside your form tag as
echo "<form action=request_material.php method=post>";
echo "<td><input type=text name=qty value=qty size=5></td>";// add inside it
echo "<input type=hidden name=materialcode value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</form>";
Please try this: I have updated the code:
echo "<table border=1>";
echo "<tr>";
echo "<th>Material Name</th>";
echo "<th>Material Description</th>";
echo "<th>Toxicity Level</th>";
echo "</tr>";
if(mysqli_num_rows($result)>0)
{
echo "<form action=request_material.php method=post>";
while ($arraymaterials=mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>".$arraymaterials['materialName']."</td>";
echo "<td>".$arraymaterials['materialDescrip']."</td>";
echo "<td>".$arraymaterials['materialToxicity']."</td>";
echo "<td>Enter Quantity</td>";
echo "<td><input type='text' name='qty[]' value='qty' size=5></td>";
echo "<input type=hidden name='materialcode[]' value=".$arraymaterials['materialCode'].">";
echo "<td><input type=submit value='Request'></td>";
echo "</tr>";
}
echo "</form>";
}
echo "</table>";
In request_material.php check value of $qty.It will be an array.for more details print_r($_POST) in request_material.php

store associative array to mysql database

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

Enable Not Null Textbox

I have an array of Textbox, by default they are all disabled... Some Textbox are empty some are not based on the return values of my query.
while ($imps_row1 = sqlsrv_fetch_array($stmt_line_util3,SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
echo "<td><input type='text' disabled='disabled' class='txtDis' name='txt1[]' value='".$imps_row1['qualified_borrower']."'></td>";
echo "<td><input type='text' disabled='disabled' class='txtDis' name='txt2[]' value='".$imps_row1['allowed_borrower']."'></td>";
echo "</tr>";
}
Is there any way that the moment the page loads, all the textbox that are not empty will not be disabled?
Something this might work using just php:
while ($imps_row1 = sqlsrv_fetch_array($stmt_line_util3,SQLSRV_FETCH_ASSOC))
{
echo "<tr>";
echo "<td><input type='text' ".(empty($imps_row1['qualified_borrower'])?"disabled='disabled'":'')." class='txtDis' name='txt1[]' value='".$imps_row1['qualified_borrower']."'></td>";
echo "<td><input type='text' ".(empty($imps_row1['allowed_borrower'])?"disabled='disabled'":'')." class='txtDis' name='txt2[]' value='".$imps_row1['allowed_borrower']."'></td>";
echo "</tr>";
}
Just uses a ternary statement to check if the variable you are echo'ing to the value is empty and if so echo's "disabled...".

Why doesn't this $_GET work

I am just trying to get the values from a table, but for some reason GET isn't working for me, or I am doing something wrong. Here is how I create my table in one php file:
<?php
.
.
.
.
echo "<tr>";
echo "<td>Login ID</td>";
$j=1;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Name</td>";
$j=2;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password</td>";
$j=3;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Birthday</td>";
$j=4;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Address</td>";
$j=5;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Email</td>";
$j=6;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Phone Number</td>";
$j=7;
echo "<td><input name=$j type='text' value='$line[$j]' size=20/></td>";
echo "</tr>";
?>
The names should be the number 1-7 right?
In another php file I attempt to access the values in those fields with he following code:
<?php
.
.
.
.
$login_id = $_GET['1'];
$name = $_GET['2'];
$pw = $_GET['3'];
$bday = $_GET['4'];
$address = $_GET['5'];
$email = $_GET['6'];
$phno = $_GET['7'];
echo "new: $login_id, $name, $pw, $bday, $address, $email, $phno";
?>
Here is what I end up getting back: new: , , , , , ,
So what is it that I am doing wrong? I can't seem to find anything wrong with my code. I know I should probably be using $_POST for the password.
<?php
echo "<form action='otherfile.php' method='get'><tr>";
echo "<td>Login ID</td>";
$j=1;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Name</td>";
$j=2;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Password</td>";
$j=3;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Birthday</td>";
$j=4;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Address</td>";
$j=5;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Email</td>";
$j=6;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Phone Number</td>";
$j=7;
echo "<td><input name=$j type='text' value='$j' size=20/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Submit Data</td>";
echo "<td><input name='submitdata' type='submit' value='submit' size=20/></td>";
echo "</tr><form>";
?>
if you want submit then get the all value in otherfile.php file
try its working...
You have not put the <form> tag. Add tag in begining
<form action='another_page.php' method='get'>
and in the end write
<input type='submit' name='submit>
</form>
What it will do on submit it will forward the values to the another page.
<form name="login" method="get" action="youraction.php">
echo "<td><input name=$j type='text' value=$j /></td>";
</form>
youraction.php
---------------
<?php
if($_GET)
{
print_r($_GET);
}
?>

Categories