html forms and php populate from Mysql - php

I have created a html form that is populated by a mysql query. The data has spaces in it.
eg "Bob site", "Sarah home"
When the form is created in the browser I get this:
Bob
site
Sarah
home
Instead of
Bob site
Sarah home.
I know I need to format my data but can't find any examples. My form code is below.
<?php include("includes/header.php"); ?>
<?php
$site = mysql_query("select distinct `site_name` from sept_billing");
?>
<html>
<body>
<h2>Create a new install</h2>
<form method="post" action="actions/newmachine_action.php">
<table border = '0'>
<tr>
<td><b>Site name:</b></td>
<td><input id="site" type="text" name="site" size="40" list="sites" value = 'Choose site name' />
<datalist id="sites">
<?php
while ($row = mysql_fetch_array($site))
{
echo "<option value=".$row[0].">";
}
?>
</datalist></td>
</tr>
</table>
<input type="submit" value="Create" />
</form>
</body>
</html>
<?php include("includes/footer.php"); ?>

Please change this and let me know if this works
echo "<option value=".$row[0].">";
to
echo "<option value='".$row[0]."'>";

This will work
<?php
while ($row = mysql_fetch_array($site))
{ ?>
<option><?php echo $row['name']; ?></option>
<?php } ?>

try
"<option text='".$row[0]."' value='".preg_replace(' ', '-', $row[0])."'></option>"
or
"<option value='".preg_replace(' ', '-', $row[0])."'>".$row[0]."</option>"
i think that value cannot have spaces

Related

<option> is having each option its own dropdown

I tried putting the loop inside but then when I pick a category it won't print in the echo and if the loop is outside like right now each category is printed alone I want them all in one tab and one go button
<?php
while($row = mysqli_fetch_array($result)) {
$category = $row["category"];
?>
<form action="" method="post">
<select name="work_place">
<option value="<?php echo $category;?>"><?php echo $category;?></option>
</select>
<input type="submit" value="go" />
</form>
<?php
}
<?php
$post = (isset($_POST['work_place'])) ? $_POST['work_place'] : '';
echo $post;
?>
Image of current issue:
<form action="" method="post">
<select name="work_place">
<?php
while($row = mysqli_fetch_array($result)) {
$category = $row["category"];
?>
<option value="<?php echo $category;?>"><?php echo $category;?>
</option>
<?php
} ?>
</select>
<input type="submit" value="go" />
</form>
You want to loop over 'option', only.
You do not want multiple forms and 'select' inputs.

Multiple selects are not being submitted in the form using post?

I am trying to create a form where I need to allow user to select different values (from select tag) against some labels.
I have two different array in php.
the standard array that contains the labels for select.
a set of values to be selected against the labels.
My problem is when I press the submit button, the form is submitted but $_POST does not show any value selected by the select tag. I want to get the selected values against the labels.
here is my code:
<?php
$data = array ('name', 'phone', 'address');
$values = array('a','2344','xyz');
?>
<html>
<head></head>
<body>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<?php for($i = 0; $i < count($data); $i++){ ?>
<label for='<?php $data[$i]?>'> <?php echo $data[$i]?></label>
<select name='<?php $data[$i]?>' id = '<?php $data[$i]?>'>
<?php foreach($values as $val){ ?>
<option value='<?php $val ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
<br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
echo$_POST['name'];
}
?>
When I press the submit button the error I get is "Notice: Undefined index: name". I have extensively searched in the questions already posted about multiple select statements but none of the answers matched my criteria. Thanks for the help.
You are forgot to print variables
<html>
<head></head>
<body>
<form action="<?php $_SERVER["PHP_SELF"] ?>" method="post">
<?php for($i = 0; $i < count($data); $i++){ ?>
<label for='<?php echo $data[$i]?>'> <?php echo $data[$i]?></label>
<select name='<?php echo $data[$i]?>' id = '<?php echo $data[$i]?>'>
<?php foreach($values as $val){ ?>
<option value='<?php echo $val ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
<br>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])){
echo$_POST['name'];
}
?>
You have not written echo before each variable in selectbox:
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<?php for($i = 0; $i < count($data); $i++) { ?>
<label for='<?php echo $data[$i]; ?>'> <?php echo $data[$i]?></label>
<select name='<?php echo $data[$i]; ?>' id = '<?php echo $data[$i]; ?>'>
<?php foreach($values as $val){ ?>
<option value='<?php echo $val; ?>'> <?php echo $val ?> </option>
<?php } ?>
</select>
<?php } ?>
<button type="submit" name="submit" value="submit">Submit</button>
</form>

displaying html checkbox multiple checkbox content in php page using loop

//I extracted data from database like
<form action="print.php" method="post">
<?php include('connection.php') ?>
<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_numrows($sql);
?>
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
<input type="checkbox" name="list[]" value="<?php echo "$f6";?>" /><?php echo "$f6","<br>"; ?>//display result in htmlpage
<?php
$i++;
$d=$f6;
}
?>
<input type="submit" value="submit"/>
</form>
print.php
//after submitting in php page
<?php include('connection.php') ?>
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['list']))//name of checkbox in html
{
foreach($_POST['list'] as $selected){
echo $selected."</br>";
}
}
}
?>
//display nothing in php...how I solve this problem
I want to display content of checkbox from html form to php page.I extracted the checkbox contents from database.problem is how I display the content in php page selecting multiple checkbox.
First of all add error_reporting in your code:
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
You have few issues in your HTML/PHP combination:
Issues:
You are using <input type="checkbox" .. inside the PHP.
your concatenation is wrong "$f6","<br>"
Also need to add name for like name="submit" in button.
Modified Code:
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
?>
<input type="checkbox" name="list[]" value="<?php echo $f6;?>" /><?php echo $f6; ?><br/>
<?php
$i++;
$d = $f6;
}
?>
Side note:
Suggestion of error_reporting is only for development and staging not for production.
Please use mysqli_* or PDO because mysql_* is deprecated and not available in PHP 7.
<?php echo "$f6","<br>"; ?>
wrong concatenation
<?php echo "$f6"."<br>"; ?>
use this
Your code should be:-
<?php
// This line should be first.
// You have missed semicolon here.
include('connection.php');
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_numrows($sql);
?>
<form action="print.php" method="post">
<?php
$i=0;
while ($i < $num) {
$f6=mysql_result($sql,$i,"item_name");
?>
<!-- You should write below line as -->
<input type="checkbox" name="list[]" value="<?php echo $f6;?>" /><?php echo $f6; ?> </br>
<?php
$i++;
$d=$f6;
}
?>
<input type="submit" value="submit"/>
</form>
there are couple of errors in your codes. please find the modified codes below with my comment started with //seeyouu:
//I extracted data from database like
<form action="print.php" method="post">
<?php include('connection.php');
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM nokia");
$num = mysql_num_rows($sql);//seeyouu: used wrong function>>mysql_numrows
$i=0; //seeyouu: after my amendment, probably can remove this
while ($row = mysql_fetch_array($sql)) {
//seeyouu: no such way of doing, your $sql already a result >> $f6=mysql_result($sql,$i,"item_name");
//seeyouu: forgot to close your php tag here
?>
<input type="checkbox" name="list[]" value="<?php echo $row["item_name"]; ?>" />
<?php //seeyouu: wrong>>echo "$f6","<br>";
//correct way:
echo $row["item_name"]."<br />";
//$d=$f6;i don't know what is this line for, so i leave it to you.
}
?>
<input type="submit" name="submit" value="submit"/> <!-- seeyouu: forgot to set name: submit -->
</form>
print.php
//after submitting in php page
<?php include('connection.php');
if(isset($_POST['submit']))
{
if(!empty($_POST['list']))//name of checkbox in html
{
foreach($_POST['list'] as $selected)
{
echo $selected."</br>";
}
}
}
?>
//display nothing in php...how I solve this problem

PHP - post foreach content

So, i have this foreach loop that runs a sql query. Every row is printed in a option. I have two forum posts. One that 'deletes' the row and one that 'uses' the row. But when I post the form the content inside the option remains empty. Here is my code:
Post file
<?php
try {
$DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);
$sql = "SELECT * FROM message";
?>
<html>
<form action="action.php" method="post">
<select><?php
foreach ($DB->query($sql) as $row)
{
?>
<option name="content" value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
<?php } ?>
</select>
<br /><input type="submit" value="use" name="use">
<input type="submit" value="delete" name="delete">
</form>
</html>
Action.php
<?php
require_once 'hidden/session.php';
$delete = $_POST['delete'];
$use = $_POST['use'];
$content = $_POST['content'];
try {
$DB = new PDO ('mysql:host=localhost;dbname=pre_messages', $DBuser, $DBpassword);
$delete = "DELETE FROM message WHERE title='$content'";
if (isset($delete)){
$DB->exec($delete);
}
}
catch (PDOException $e)
{
echo $e->getMessage();
}
In order to post content you have to give name to your control. You have specified name property for option but there is no name property specified in select. Which is why the value of that control is not getting posted. Hence, when you try to access it as $content = $_POST['content']; , it gives you empty value.
Try this:
<html>
<form action="action.php" method="post">
<select id="content" name="content">
<?php foreach ($DB->query($sql) as $row) { ?>
<option value="<?php echo $row['Title']; ?>"><?php echo $row['Title']; ?></option>
<?php } ?>
</select>
<br />
<input type="submit" value="use" name="use">
<input type="submit" value="delete" name="delete">
</form>
</html>
Hope it helps!!

php script with multiple buttons

The idea is the page in which you select a category from drop-down menu, then after you click remove button, new form shows(contains yes/no buttons) that asks you if you really want to remove selected category. Problem is the second yes/no script. It works on separate page, but on page with first form it doesn't echo anything nor does it remove a pet. Please help, thanks!
<?php
/*remove a category*/
include("connection.php");
?>
<html><head></head>
<body>
<?php
$PetListquery= "SELECT distinct petType From petType ORDER by petType";
$PetListResult= mysqli_query($cxn,$PetListquery) or die ("Couldn't execute query.");
?>
<div style="border:2px solid;">
<form method="POST" action="removeCategory.php">
<div align='left'>
Choose category you want to remove:
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' ><?php echo $petType;?> </option>
<?php }?>
</select>
</div>
<div>
<p><input type='submit' name='Remove' value='Remove Category' />
</div>
</div>
</form>
<?php
foreach($_POST as $field => $value)
{ //second form starts after if
if($field == 'petType')
{
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<div>
<input name="Yes" type="submit" value="Yes">
<input name="No" type="submit" value="No">
</div>
</form>
<?php
echo "Are you sure you want to delete selected category?";
//clicking any of these buttons doesn't display anything
if(isset($_POST['Yes']))
{
echo "yes";
$DeleteQuery= "DELETE From petType WHERE petType='$petType'";
$DeleteResult= mysqli_query($cxn,$DeleteQuery) or die ("Error1!");
}
if(isset($_POST['No']))
{
echo "No!";
}
}
}
?>
</body></html>

Categories