<?php
include("dbFunctions.php");
$query ="SELECT * FROM `physical_examination` WHERE `PE_Opt_ans`= 0";//select form options name
$result = mysqli_query($link,$query);
?>
<div id="tabs-3">
<table>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="tab0">
<?php while ($arrayResult = mysqli_fetch_array($result)){ ?>
<tr>
<td><label for="input"><b><?php echo $arrayResult['PE_Opt_name']?></label></td>
<?php if ($arrayResult['PE_Opt_type'] == "textarea") { ?>
<td><textarea rows="8" cols="45"name = "other1"></textarea></td>
<?php } else { ?>
<td><input type="<?php echo $arrayResult['PE_Opt_type']?>" name="input<?php echo $arrayResult['id']?> " ></td>
</tr>
<?php } ?>
<?php } ?>
<br> <input value="Submit" type="submit" name="submit1">
</form>
</table>
<?php
include "dbFunctions.php";
if(isset($_POST['submit1'])) {
$number = $_POST['other1'];
I am stuck here.
How do I $_POST the form based on the while loop after I click on submit button? Do I need another while loop again for the name value of input <?php echo $arrayResult['id']?>
I didn't quiet get what you want but i assume you want the contents of the textarea generated by the while loop.
Change in your loop the names from input to input[] and other1 to other1[]
If you var_dump($_POST) afterwards you should see that they are now an array which you can loop over with foreach
PS: You should NEVER EVER eat raw $_POST/$_GET inputs and PHP_SELF is another security breach. But that's just my two cents
Related
I need to display an HTML table with the data that i get from a PhP form. I did a thing like this but It doesn't work properly. It shows the data row on top of the title of the columns. Here's the code:
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Nome: <input type="text" name="nome">
Ragione sociale: <input type="text" name="rag">
Indirizzo: <input type="text" name="ind">
Partita IVA: <input type="text" name="iva">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_REQUEST['nome']);
$rag = htmlspecialchars($_REQUEST['rag']);
$ind = htmlspecialchars($_REQUEST['ind']);
$iva = htmlspecialchars($_REQUEST['iva']);
}
?>
<table>
<tbody>
<tr>
<?php
$a = array($name,$rag,$ind,$iva);
for ($i=0; $i<count($a); $i++){
print_r($a[$i]);
echo " ";
}
?>
</tr>
<tr>
<td>|Nome|</td>
<td>Ragione Sociale|</td>
<td>Indirizzo|</td>
<td>Partita IVA|</td>
</tr>
</tbody>
</table>
</body>
</html>
You print your data in the first row, without outputting any <td> for your data. Then in the second row you print your titles/headers. Change your table like so, see if that's what you want:
<table>
<thead>
<tr>
<th>|Nome|</th>
<th>Ragione Sociale|</th>
<th>Indirizzo|</th>
<th>Partita IVA|</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$a = array($name,$rag,$ind,$iva);
for ($i=0; $i<count($a); $i++){
echo "<td>";
echo $a[$i];
echo "</td>";
}
?>
</tr>
</tbody>
</table>
More on tables: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table
Edit: I'd also suggest to place your table inside the if ($_SERVER["REQUEST_METHOD"] == "POST") { so it only gets printed after you submitted the form
i want to pass an array value from input name that generated from an array. i can view $_POST if i manually define on script.
$stmt = $db->prepare($sql);
$stmt->execute();
$i = 0;
while ($row = $stmt->fetch())
{
$id=strtoupper($row["id"]);
$nama=strtoupper($row["nama"]);
$sn=strtoupper($row["sn"]);
$kewpa=strtoupper($row["kewpa"]);?>
<form method="post" action="kewpaupdate.php"><tr>
<td><input type="text" name="id[<?php echo $i;?>]" value=<?php echo $id;?>></td>
<td></td>
<td></td>
<td><input type="text" name="kewpa[<?php echo $i;?>]" value=<?php echo $kewpa;?>></td>
<td align="center"><input type="submit" value="submit"></td>
</tr></form>
<?php ++$i;
?>
</tbody>
</table>
<?php
$w = $_POST['id'];
$r = $_POST['kewpa'];
echo $w;
echo "<br>";
echo $r;
?>
how to display array value that choose from submit button.
You need to do 2 things:
1) As you are printing the $_POSTed data on the page itself, you need to submit the form to same page.
Please change <form> action to blank.
Changed form action code:
<form method="post" action="">
This will submit the form to same page.
2) As your input has name of type array, you can't just print it with echo or print().
These are used for printing strings.
You need to use print_r().
So, please use:
echo '<pre>';
print_r($_POST['id']);
echo '<pre>';
You will get an array, same for the other field.
I fetch total_amount value and insert into another table but i want to insert total_amount in one field and seperated by ",". How to do that?
I know Mysqli is latest version . but here mysql is working properly.
<?php
include('database/db.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$sql1="Insert into test(`total_amount`) values ('{$_POST['i']}')";
$result1=mysql_query($sql1);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Page</title>
</head>
<body>
<form name="" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<table>
<tr>
<td>Total Amount</td>
</tr>
<?php
$sql="Select * from tes1";
$result=mysql_query($sql);
while($dtset=mysql_fetch_array($result))
{
?>
<tr>
<td><input type="hidden" name="i" value="<?php echo $dtset['total_amount'];
?>"><?php echo $dtset['total_amount']; ?></td>
</tr>
<?php } ?>
</table>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<?php
$sql="Select * from tes1";
$result=mysql_query($sql);
// Empty array
$amounts = [];
while($dtset=mysql_fetch_array($result))
{
// Push the amounts to the array.
$amounts[] = $dtset['total_amount'];
}
// Implode will create a string from your array and seperates it with
// the chosen character
$combined = implode(",",$amounts);
?>
<tr>
<td>
<input type="hidden" name="i" value="<?php echo
$combined; ?>">
<?php echo $combined; ?>
</td>
</tr>
The while loop is now not part of the table row, but instead creates the array with all values first and then you can add the string created by implode to your hidden form field.
I am trying to insert multiple rows to a database table if check box is selected. But in my code when I am trying to insert, new rows are inserting based on check box selection. But no data is passing. I need some advice on below code to modify:
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$qry="select * from pi";
$result=mysql_query($qry);
?>
<form action="check.php" method="post">
<table>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr><td><input type=checkbox name=name[] value='".$row['id']."'>".$row['PI_NO']."</td><td>".$row['CUSTOMER_NAME']."</td><td>".$row['PI_ADDRESS']."</td></tr>";
}
?>
<input type="submit" value="save" id="submit">
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$name=$_POST['name'];
foreach($_POST['name'] as $x)
{
$qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($qry);
}
?>
Notes:
You forgot to bind the name of your checkbox using a single tick (')
You used variables in your query which you didn't defined and assigned value with yet
You only passed on the value of name, and did not include the Pi Address and Customer name. I'll be passing them by hidden input using <input type="hidden">.
I'll change the way you check your passed on form by looping them and check them using for() and if()
Use mysql_real_escape_string() before using them in your queries to prevent some of the SQL injections. But better if you consider using mysqli prepared statement rather than the deprecated mysql_*.
Is your post a single file? If it is, you must enclose your query using an isset() to prevent error upon loading the page.
You didn't close your <form>
Here's your corrected while loop:
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td>
<input type="checkbox" name="name[]" value="<?php echo $row['id']; ?>">
<?php echo $row["PI_NO"]; ?>
<!-- HERE IS THE START OF YOUR TWO HIDDEN INPUT -->
<input type="hidden" name="piaddress[]" value="<?php echo $row["PI_ADDRESS"]; ?>">
<input type="hidden" name="customer[]" value="<?php echo $row["CUSTOMER_NAME"]; ?>">
</td>
<td><?php echo $row['CUSTOMER_NAME']; ?></td>
<td><?php echo $row['PI_ADDRESS']; ?></td>
</tr>
<?php
} /* END OF WHILE LOOP */
?>
<input type="submit" value="save" id="submit">
</form> <!-- YOU DID NOT CLOSE YOUR FORM IN YOUR POST -->
And your query:
<?php
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$counter = count($_POST["name"]); /* COUNT THE PASSED ON NAME */
for($x=0; $x<=$counter; $x++){
if(!empty($_POST["name"][$x])){
$PI_NO = mysql_real_escape_string($_POST["name"][$x]);
$CUSTOMER_NAME = mysql_real_escape_string($_POST["customer"][$x]);
$PI_ADDRESS = mysql_real_escape_string($_POST["piaddress"][$x]);
$qry="INSERT INTO pi (PI_NO, CUSTOMER_NAME, PI_ADDRESS) VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($qry);
} /* END OF CHECKING THE CHECKBOX IF SELECTED */
} /* END OF FOR LOOP */
?>
Lots of little problems. And some big ones.
as $x){ .. $x is not being used so I assume you just loop for the number of checked boxes.
These have no values: '$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS'
Missing </form>
Not being used: $name=$_POST['name'];
<?php
echo '<form action="check.php" method="post"><table><tr><th>A</th><th>B</th><th>C</th></tr>';
$db=mysql_connect("localhost","root","");
mysql_select_db("kkk",$db);
$sql = "select `id`,`PI_NO`, `CUSTOMER_NAME` ,`PI_ADDRESS` from `pi`";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo "<tr><td><input type=\"checkbox\" name=\"name[]\" value=/"$row[0]/"'>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>";
}
echo '<input type="submit" value="save" id="submit"></form>';
foreach($_POST['name'] as $x){
$sql="INSERT INTO pi (`PI_NO`, `CUSTOMER_NAME`, `PI_ADDRESS`)VALUES ('$PI_NO','$CUSTOMER_NAME','$PI_ADDRESS')";
mysql_query($sql);
}
?>
On a Previous page the user enters in a number in the textbox, when that form is submitted, the number is posted on this page as $_SESSION['sessionNum']. Now I want to store this number on the top as $sessionMinus showing the same number except difference is that everytime this form is submitted to it self, the number counts down by 1 everytime the form is submitted. Problem is that it is giving me an undefined variable $sessionMinus. How can this be fixed?
<?php
session_start();
//validate the post data if necessary
if (isset($_POST['sessionNum'])) {
$_SESSION['sessionNum'] = $_POST['sessionNum'];
}else{
$_SESSION['sessionNum']--;
}
$sessionMinus = $_SESSION['sessionNum'];
?>
<body>
<?php echo $sessionMinus; ?>
<form id="enter"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
method="post"
onsubmit="return validateForm(this);" >
<p>
<input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" />
</p>
</form>
<?php
$outputDetails = "";
$outputDetails .= "
<table id='sessionDetails' border='1'>
<tr>
<th>Number of Sessions:</th>
<th>{$_SESSION['sessionNum']}</th>
</tr>";
$outputDetails .= "</table>";
echo $outputDetails;
?>
</body>
Your $sessionMinus is never initialized.
When you do isset($sessionMinus) you're checking if the variable has been declared, and since it hasn't, it will of course be undefined. If you want variables to be carried across each submit, you have to store it in a $_SESSION variable, not a local variable.
You can learn about variable scope here and about session variables here.
You're using $_POST
You're using $_POST but you're not sending a variable named sessionNum in your form.
Keep it clean
You should also try to keep your html as "clean" as possible, by separating PHP processing and PHP outputting.
This is how your setup should look like
<?php
session_start();
if(!isset($_SESSION['my_counter'])){
if(isset($_POST['my_count'])){
//Declare my counter for the first time
$_SESSION['initial_count'] = $_POST['my_count'];
$_SESSION['my_counter'] = $_POST['my_count'];
}
}else{
//Decrement my counter
$_SESSION['my_counter']--;
}
$actionurl = htmlentities($_SERVER['PHP_SELF']);
if($_SESSION['my_counter'] <= 0)
$number_of_sessions = "No sessions left!";
else
$number_of_sessions = $_SESSION['my_counter'];
$started_with_sessions = $_SESSION['initial_count'];
?>
<!DOCTYPE html>
<body>
<form id="enter" action="<?php echo $actionurl; ?>" method="post" onsubmit="return validateForm(this);" >
<input type='hidden' name='my_count' value='5' />
<input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" />
</form>
<table id='sessionDetails' border='1'>
<tr>
<th>Number of sessions: </th>
<th><?php echo $number_of_sessions; ?></th>
</tr>
<tr>
<th>Started with: </th>
<th><?php echo $started_with_sessions; ?> sessions</th>
</tr>
</table>
</body>
</html>
if $sessionMinus is not set, you are never setting it. Perhaps you should be checking if it's not set before you set it?
if (!isset($sessionMinus)) {
$sessionMinus = "";
$sessionMinus .= $_POST['sessionNum']--;
}