Enable Not Null Textbox - php

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

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'];

Multiplying $_POST

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.

Getting values of input boxes in a table using jquery

I have a table containing multiple rows of input boxes. Entire table and its contents are created dynamically using jQuery Post. Each tr in the table has 7 td and the first td contains a radio button and remaining have text input boxes with values. User has to change the values in the text boxes and click on radio button to update the same. how to get the value if input boxes in the selected row?
I used following function to pop an alert message
jQuery(document).delegate("[name='edit']", "click",function (e) {
alert("clicked");}
This is working. I want to get values of input boxes using this. closest tr input . But do not know the syntax. Please help
the script for creating the table is like this:
echo'<form id="shiftentry" name="shiftentry" >';
echo "Date:<input id=shiftdate value='".$date."'/>";
echo "Unit:<input id=shiftdate value='".$unit."'/>";
echo "Shift:<input id=shiftdate value='".$shift."'/><br/>";
echo "<table><tr><th>No</th><th>Ele</th><th>Location</th><th>Drate </th><th>H3 Val </th><th>Part </th> <th>Iod</th><th>Cont. </th></tr>";
while($row2=mysql_fetch_array($result_sec))
{
echo "<tr>";
echo "<td><input name='edit' type='radio'/></td>";
echo "<td>".++$rc."</td>";
echo "<td><input size='5' id='".$row2['ele']."' value='".$row2['ele']."' /></td>";
echo "<td><input id='".$row2['loc_id']."' value='".$row2['loc']."' /></td>";
echo "<td><input size='5' id='drate".$rc."' value='".$row2['drate']."'/></td>";
echo "<td><input size='5' id='h3".$rc."' value='0' /></td>";
echo "<td><input size='5' id='part".$rc."' value='0' /></td>";
echo "<td><input size='5' id='iod".$rc."' value='0' /></td>";
echo "<td><input size='5' id='cont".$rc."' value='0' /></td>";
echo "</tr>";
}
echo " </table>";
echo '<div align="center">';
echo '<input type="submit" id="submit2" name="submit2" value="Submit" width="30" />';
echo '</div>';
echo '</form>';
Take a look on .closest() and :eq() selector.
jQuery(document).delegate("input[name='edit']", "click",function (e) {
// Will return the Parent Row of clicked input
var _tr = $(this).closest('tr');
// You have 9 td in particular row
// Let's access the 7th input box: <input size='5' id='iod".$rc."' value='0' />
$('td:eq(7) input', _tr).val(); // will return its value
});

track change in an array then submit the array to the same page

I have the following php code (I omitted the irrelevant part of code):
<?php
echo "<form action=$_SERVER[PHP_SELF] method=\"POST\">";
echo "<table>";
for ($i=0;$i<$num_rows;$i+=1) {
echo "<tr>";
echo "<td align=center>$variable1[$i]</td>";
echo "<td align=center>$variable2[$i]</td>";
echo "<td align=center><input type=\"text\" name=\"variable3[$i]\" value=\"$variable3[$i]\" /></td></tr>";
echo "<input type=\"hidden\" name=\"variable1[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable2[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable3[$i]\" value=\"$variable1[$i]\">";
}
echo "<tr><td colspan=\"3\"><input name=\"action\" type=\"submit\" value=\"Update\"></td></tr>";
echo "</table>";
echo "</form>";
?>
I want to pass the variables through POST method to the same page. The code retrieves data from the database and lists it in a table. However, I want to be able to manually modify variable3 and submit the array after I do this. The problem is that the array is submitted before I modify the variable3. How can I submit the array with the value of variable3 modified? How can I check if variable3 was modified and do the submitting afterwards? Thanks.
delete line 11 like this
<?php
echo "<form action=$_SERVER[PHP_SELF] method=\"POST\">";
echo "<table>";
for ($i=0;$i<$num_rows;$i+=1) {
echo "<tr>";
echo "<td align=center>$variable1[$i]</td>";
echo "<td align=center>$variable2[$i]</td>";
echo "<td align=center><input type=\"text\" name=\"variable3[$i]\" value=\"$variable3[$i]\" /></td></tr>";
echo "<input type=\"hidden\" name=\"variable1[$i]\" value=\"$variable1[$i]\">";
echo "<input type=\"hidden\" name=\"variable2[$i]\" value=\"$variable1[$i]\">";
}
echo "<tr><td colspan=\"3\"><input name=\"action\" type=\"submit\" value=\"Update\"></td></tr>";
echo "</table>";
echo "</form>";
?>
Now you can see if it has been changed by cheking the value of $_POST['variable3'].

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

Categories