$_POST not running for the form inside PHP script - php

I was making a form inside a PHP script and when i tried to access a field
of form in the same script it was giving me error Undefined index
This is my code:
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
echo '<form method="POST">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
}
else
{
echo 'No vendor to display';
}
echo $_POST['book_name'];

USE THIS:-
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
echo '<form method="POST">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
}
else
{
echo 'No vendor to display';
}
if($_POST){
echo $_POST['book_name'];
}
?>

Try this
<?php
$query="SELECT * FROM `vendor_list`";
$query_run=mysql_query($query);
if(mysql_num_rows($query_run)>0)
{
?>
<form method="POST" action="">
Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>
Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>
<select name="vendor_email">
<?php
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
?>
</select><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
}
else
{
echo 'No vendor to display';
}
if(isset($_POST["submit"]))){
echo $_POST['book_name'];
}
?>

The Clean Way
All the breaks you have in that code are a hot mess...
Tip When you are echoing too much HTML just wrap it in an if..else.. like this...
<?php
$query = "SELECT * FROM `vendor_list`";
$query_run = mysql_query($query);
?>
<?php if(mysql_num_rows($query_run)>0): ?>
<form method="POST">
<label for="book_name">Book name:</label>
<input type="text" name="book_name" id="book_name" size="40" maxlength="40">
<label for="book_name"Number Of Copies:</label>
<input type="text" size="5" name="num_copies" maxlength="2">
<select name="vendor_email">
<?php while($query_row=mysql_fetch_assoc($query_run)): ?>
<option value="<?=$query_row['email']?>"><?=$query_row['vendor_name']?></option>
<?php endwhile; ?>
</select>
<input type="submit" value="submit">
</form>
<?php else: ?>
<p>No vendor to display</p>
<?php endif; ?>
<?php if (isset($_POST['book_name'])) echo $_POST['book_name']; ?>

Using the below code this will also check if you have a form submission or not, unless this code is a cut/copy paste.. It looks like it will process sections when it should not.. eg the echo post..
Personally i myself will set the action="" to PHP_SELF as many years ago had issues with it, soon this will change once i start using HTML5 when its cross browser enough / older users update their browsers.
Example of processing or showing a post but not both.
echo $_POST['book_name'];
echo "POST Dump: " . print_r($_POST, true);
} else {
// No Form Submission, So we generate the form instead
// QUERY - data
$query = "SELECT * FROM 'vendor_list'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)>0) {
// FORM - Name/#Copies
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
// FORM - Select Vendor
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
// FORM - Submit / Close Form
echo '<input type="submit" value="submit">';
echo '</form>';
} else {
echo 'No vendor to display';
}
}
?>
Or using a code that shows the form and processing any $_POST
<?php
// QUERY - data
$query = "SELECT * FROM 'vendor_list'";
$query_run = mysql_query($query);
if(mysql_num_rows($query_run)>0) {
// FORM - Name/#Copies
echo '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
echo 'Book name:<input type="text" name="book_name" size="40" maxlength="40"><br><br>';
echo 'Number Of Copies:<input type="text" size="5" name="num_copies" maxlength="2"><br><br>';
// FORM - Select Vendor
echo '<select name="vendor_email">';
while($query_row=mysql_fetch_assoc($query_run))
{
$laser=$query_row['vendor_name'];
$email=$query_row['email'];
echo "<option value='".$email."'>".$laser."</option>";
}
echo '</select><br>';
// FORM - Submit / Close Form
echo '<input type="submit" value="submit">';
echo '</form>';
} else {
echo 'No vendor to display';
}
if (isset($_POST['book_name'])) {
echo "<b>Displaying post data</b> <br />";
echo "Example book: " . $_POST['book_name'] . "<br />";
echo "POST Dump: <br /><pre>" . print_r($_POST, true) . "</pre>";
} else {
echo "Page has not been subitted yet, please select an item";
}
?>

Related

How to create a selection dropdown in php form

I am working on a project and would like to give the user per-determined values when updating a record.
Here is my code so far.
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// connect to the database
include('connect-db.php');
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{
// confirm that the 'id' value is a valid integer before getting the form data
if (is_numeric($_POST['id']))
{
// get form data, making sure it is valid
$id = $_POST['id'];
$Name = mysql_real_escape_string(htmlspecialchars($_POST['Name']));
$Status = mysql_real_escape_string(htmlspecialchars($_POST['Status']));
$Comments = mysql_real_escape_string(htmlspecialchars($_POST['Comments']));
$Type = mysql_real_escape_string(htmlspecialchars($_POST['Type']));
// check that firstname/lastname fields are both filled in
if ($Name == '' || $Type == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($id, $Name, $Status, $Comments, $Type, $error);
}
else
{
// save the data to the database
mysql_query("UPDATE Schools SET Name='$Name', Status='$Status', Comments='$Comments', Type='$Type' WHERE id='$id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: view.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error!';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
// query db
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM Schools WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$Name = $row['Name'];
$Status = $row['Status'];
$Comments = $row['Comments'];
$Type = $row['Type'];
// show form
renderForm($id, $Name, $Status, $Comments, $Type, '');
}
else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error!';
}
}
?>
I am wanting to replace the status text filed with a drop down list of options.
Replace your <input by <select :
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<!-- <strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>-->
<strong>Status:</strong> <select name="Status">
<option value="1">Status 1</option>
<option value="2">Status 2</option>
</select>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
If your statuses are in a table, fill the <select> with a query :
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br>
<!-- <strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>-->
<strong>Status:</strong> <select name="Status">
<?php
$result = mysql_query("SELECT * FROM tbl_status",$cnx);
while ( $row = mysql_fetch_array($result) )
echo "<option value='" . $row["id"] . "'>" . $row["text"] . "</option>";
?>
</select>
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br>
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br>
<input type="submit" name="submit" value="Submit">
</div>
</form>
You could use the html <datalist> or the <select> tag.
I hope I could help.
First of all you need to switch from mysql_* to mysqli_* as it going to get removed in php 7.0 I'm using this function i created and it might help you
here is the php code
function GetOptions($request)
{
global $con;
$sql = "SELECT * FROM data GROUP BY $request ORDER BY $request";
$sql_result = mysqli_query($con, $sql) or die('request "Could not execute SQL query" ' . $sql);
while ($row = mysqli_fetch_assoc($sql_result)) {
echo "<option value='" . $row["$request"] . "'" . ($row["$request"] == $_REQUEST["$request"] ? " selected" : "") . ">" . $row["$request"] . "$x</option>";
}
}
and the html code goes like
<label>genre</label>
<select name="genre">
<option value="all">all</option>
<?php
GetOptions("genre");
?>
</select>

A simple operation but it giving an error I want to update each id

It is simple but I am not getting values of radio buttons for updation when I press submit.
It is giving an error in foreach statement like :Warning: Invalid argument supplied for foreach() Please say how to get the values of radio button after pressing submit button,
here I am updating the status of state .
<?php
include("db.php");
?>
<html>
<body>
<?php
$state=$_POST['state'];
if(isset($_POST['submit']))
{
$result1 = mysql_query("select state,id,status from states ");
while($rr1=mysql_fetch_array($result1))
{
//getting values of radio buttons
$myval=$rr1['id'];
echo $myval;
$val=$_POST['yes.$rr1["id"]'];
echo $val;
$val1=$val.$myval;
$vall=yes.$rr1['id'];
foreach($vall as $values)
{
echo $values;
$update=mysql_query("UPDATE states SET status='". $values."'
WHERE id='$myval' ");
}
}
}
echo "ok";
?>
<form action="" name="form" id="form" method="post" >
<?php
//session_start();
include("db.php");
$result = mysql_query("select state,id,status from states ");
while($info1=mysql_fetch_assoc( $result))
{
echo $info1['city'];
echo "<br>";
/*echo "<br>";
echo "company Name:".$info1['company_name'];
echo "<br>";
echo "salary:".$info1['maxsalary'];
echo "<br>";
//echo $info1['company_name'];*/
?>
<label><?php echo $info1['state']; ?></label>
<input type="radio" <?php if($info1['status']=="yes"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="yes">Yes
<input type="radio" <?php if($info1['status']=="no"){ echo
"checked='checked'"; } ?> name="yes.<?php echo $info1[ 'id']; ?>[]"
value="no">no
<br/>
<?php } ?>
<br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
I think you need to write
$vall=yes.$rr1['id'];
to
$vall="yes".$rr1['id'];
Thanks

Error while editing and saving the data in PHP

When I delete the values it works well. When I Search it works well.
Only when I try to save the data after edit I get an error.
I also tried to use
if(!isset($_GET['edit']))
{
echo '<form action="" method="post">';
echo '<input type="text" name=criteria>';
echo '<input type="submit" value="search" name="submit">';
echo '</form>';
}
But it did not work.
The error I receive is ( ! ) SCREAM: Error suppression ignored for
( ! ) Notice: Undefined index: criteria in C:\wamp\www\test\search.php on line 13.
The entire code is
<html>
<head>
</head>
<body>
<?php
$page='search.php';
mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("list") or die (mysql_error());
if(empty($_POST) === false)
{
$data=$_POST['criteria'];
if (empty($data) === true)
{
echo 'Please enter some text!!<br/>';
}
else
{
$get=mysql_query("SELECT SRNO, fname, lname, phone, email, address, comments from names where fname='" . mysql_real_escape_string($data) . "'");
if (mysql_num_rows($get)==0)
{
echo 'There are no search results!!';
}
else
{
echo '<table border=0 cellspacing=25 cellpadding=1>';
echo'<tr><th>Sr. No</th><th>First Name</th><th>Last Name</th><th>Phone No</th><th>E-mail</th><th>Address</th><th>Comments!!</th><th>Modify</th><th>Delete!</th></tr>';
while($get_row=mysql_fetch_assoc($get))
{
echo '<tr><td>'.$get_row['SRNO'].'</td><td>'.$get_row['fname'].'</td><td>'.$get_row['lname'].'</td><td>'.$get_row['phone'].'</td><td>'.$get_row['email'].'</td><td>'.$get_row['address'].'</td><td>'.$get_row['comments'].'</td><td>Edit</td><td>Delete</td></tr>';
}
echo '</table>';
}
}
/*
if(mysql_num_rows($getf) == 0)
{
$getel=mysql_query('SELECT SRNO, fname, lname, phone, email, address, comments from names where lname='.$_GET['$data']));
}*/
}
if(isset($_GET['delete']))
{
mysql_query('DELETE from names where SRNO='.mysql_real_escape_string((int)$_GET['delete']));
}
if(isset($_GET['edit']))
{
$getedit=mysql_query('SELECT SRNO, fname, lname, phone, email, address, comments from names where SRNO='.mysql_real_escape_string((int)$_GET['edit']));
echo '<table border=0>';
while ($get_row=mysql_fetch_assoc($getedit))
{
echo '<form method="POST" action="">';
echo '<tr><td>Sr.No:</td><td><input type="text" value='.$get_row['SRNO'].' name="SRNO" readonly="readonly"></td></tr>';
echo '<tr><td>First Name:</td><td><input type="text" value='.$get_row['fname'].' name="fname"></td></tr>';
echo '<tr><td>Last Name:</td><td><input type="text" value='.$get_row['lname'].' name="lname"></td></tr>';
echo '<tr><td>Phone No:</td><td><input type="text" value='.$get_row['phone'].' name="phone"></td></tr>';
echo '<tr><td>E-mail address:</td><td><input type="text" value='.$get_row['email'].' name="email"</td></tr>';
echo '<tr><td>Address:</td><td><textarea name="address" rows=4>'.$get_row['address'].'</textarea></td></tr>';
echo '<tr><td>Comments:</td><td><textarea name="comments" rows=4>'.$get_row['comments'].'</textarea></td></tr>';
echo '<tr><td><input type="submit" name="submit" value="save"></td><td>Cancel</td></tr>';
echo '</form>';
}
echo '</table>';
}
if(!isset($_GET['edit']))
{
echo '<form action="" method="post">';
echo '<input type="text" name=criteria>';
echo '<input type="submit" value="search" name="submit">';
echo '</form>';
}
?>
</body>
</html>
There are two forms and just one
if(empty($_POST) === false)
how do I add a condition in if statement to filter each one?
Please help
Try putting double quotes around the name on your input :
<input type="text" name="criteria">

PHP array via multiple form inputs

I've the following code :
$query = "SELECT * FROM items WHERE SUBSTRING(item_no, 1, ".$length.") BETWEEN
'".$from_new."' AND '".$to_new."' ORDER BY item_no Desc";
$result = mysql_query($query);
$dd=array();
$ii=array();
$qq=array();
$aa=array();
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
?>
<form method="post" action="final_group_items.php">
<?php
echo "<table>";
for($i=0;$i<$num;$i++){
$row = mysql_fetch_array($result);
echo "<tr><td align=center>"; ?>
<input disabled maxlength="2" type="text"
name="ii[]" value="<?php echo strtoupper($row['item_no']); ?>"><?php echo
"</td><td align=center>";?>
<input disabled maxlength="2" type="text"
name="qq[]" value="<?php echo $row['qty'];?>">
<?php echo "</td><td align=center>"; ?>
<input disabled maxlength="2" type="text"
name="aa[]" value="<?php echo $row['actual_price'];?>">
<?php echo "</td><td align=center>";?>
<input required maxlength="2" type="text" name="dd[]" value="<?php echo
$row['discount_price']; ?>">
<?php
echo "</td><tr>";
}
echo "</table>";
?>
<input type="submit" value="Change Values">
</form>
Now when i click Submit it will open the final_group_items.php which has the follow testing code to make sure if all array (ii,qq,dd,aa) are not empty:
if(empty($_POST['qq']))
{
echo "No value inside";
return false;
}
foreach($_POST['qq'] as $test)
{
echo $test;
}
return true;
so by testing all array's, the only one works is $_POST['dd']...Others outputs "no value inside" which I really don't know how or why?
What should I do when I have multiple of fields having uniqe arrays and values.
Thank You
This:
<input disabled maxlength="2" type="text" name="qq[]" value="<?php echo $row['qty'];?>">
Will prevent the browser from even posting the field at form submission, because disabled gets an implicit value of "disabled".
The attribute should be removed:
<input maxlength="2" type="text" name="qq[]" value="<?php echo $row['qty'];?>">

Dynamically Generated Radio button form when submitted does not display data

I am trying to create a quiz with PHP/Mysql...
I have created a form with radio buttons for answers which displays data pulled from the database as values for the radio buttons. I tried to submit the form but the result page does not show anything.
My quiz code goes as follows:
<form method="post" action="insertscore.php" name="cssCheckbox" id = "cssCheckbox">
<?php $query = "SELECT * FROM questions WHERE (`topics` = '.NET' OR `topics` = 'PHP') ORDER BY Rand() LIMIT 5"; $result = mysql_query($query);
if ($result && mysql_num_rows($result)) {
$numrows = mysql_num_rows($result);
$count =1;
while ($row = mysql_fetch_array($result))
{
?>
<div class="group">
<input type="hidden" name="<?php echo $row['key_id']; ?>"><?php $row['key_id']; ?></input>
<span class="test_question"><strong><?php echo $count;?>) <?php echo $row['question']; ?>
</strong><br />
<?php if($row['answer1'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer1']; ?>" id="chkLimit_1" ></input>
<label for="chkLimit_1" ><?php echo $row['answer1']; echo "<br />"; } else {} ?></label>
<?php if($row['answer2'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer2']; ?>" id="chkLimit_2" ></input>
<label for="chkLimit_2" ><?php echo $row['answer2']; echo "<br />"; } else {} ?></label>
<?php if($row['answer3'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer3']; ?>" id="chkLimit_3" ></input>
<label for="chkLimit_3" ><?php echo $row['answer3']; echo "<br />"; } else {} ?></label>
<?php if($row['answer4'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer4']; ?>" id="chkLimit_4" ></input>
<label for="chkLimit_4" ><?php echo $row['answer4']; echo "<br />"; } else {} ?></label>
<?php if($row['answer5'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer5']; ?>" id="chkLimit_5" ></input>
<label for="chkLimit_5" ><?php echo $row['answer5']; echo "<br />"; } else {} ? ></label>
<?php if($row['answer6'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer6']; ?>" id="chkLimit_6" ></input>
<label for="chkLimit_6" ><?php echo $row['answer6']; echo "<br />"; } else {} ?></label>
<?php if($row['answer7'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer7']; ?>" id="chkLimit_7" ></input>
<label for="chkLimit_7" ><?php echo $row['answer7']; echo "<br />"; } else {} ?></label>
<?php if($row['answer8'] != NULL){ ?>
<input type = "radio" name="answers" value="<?php echo $row['answer8']; ?>" id="chkLimit_8" ></input>
<label for="chkLimit_8" ><?php echo $row['answer8']; echo "<br />"; } else {} ?></label>
<input type="hidden" name="<?php echo $row['right_answer']; ?>"><?php $row['right_answer']; ?></input>
</div>
<input name="Submit" type="submit" value="Submit Your Answers" class="submit">
</form>
Code on submitted Page looks like:
<?php
if(isset($_POST['Submit'])){
$key_id=$_POST['key_id']; echo $key_id;
$question=$_POST['question']; echo $question;
$answers=$_POST['answers']; echo $answers;
$correctanswer=$_POST['correctanswer']; echo $correctanswer;
}
foreach($_POST as $key => $val)
{
echo "$key --> $val<br />";
}
//var_dump($_POST);
?>
Please let me know if anything is not clear or if I am missing anything....
Thanks,
Shank
I would:
remove comments to //var_dump($_POST); and move this line at the top of the code on submitted Page.
if you still don't see anything, I think the code on submitted page is not in a file called insertscore.php or such file is not in same folder of your form page.

Categories