why cant i add space before the buttons in PHP - php

I'm trying to add some space after the drop down but it's not working...
PHP
<?php
$connect = mysql_connect("server","username","") or die("Error connecting to MYSQL");
mysql_select_db("database") or die("Error connecting to database");
$result = mysql_query("SELECT * FROM table ORDER BY column ASC");
"<select name='drpdwn'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value=" .$row['col1'] . ">".$row['col1'] ."</option>";
}
echo "<br/>";
echo "";
?>
<br/>
<input type="submit" id="thisSbmit" value="Delete Contact" onClick="chck()"> <input type="button" id="cls" class="cls" value="Clear">
This is the code i added <br> two times and blank space once but it doesn't work..

You never actually close the <select> (with a </select>).
- echo "<br/>";
+ echo "</select><br/>";
http://jsfiddle.net/LTXrd/
vs. http://jsfiddle.net/LTXrd/1/
I do find it a bit odd that it has that effect, but I guess that's good motivation to properly format HTML.

Some problems I can see:
You are not closing the select element
input elements are inline elements so if you want to add space around them using for example css margins, you should change that to something like: input { display: inline-block; }

Related

PHP selection of drop down menu not storing in variable

I have a simple table with staff names stored in the column f_operator_name.
I have a drop down menu in a php form with these staff names available for selection. Here is a snippet of the relevant the code:
<?php
echo "<h2>Operator: <select name=f_operator_id></h2>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['operator'];
}
?>
When someone selects the staff name I want it to be stored in a variable. I'm testing the submit form at the bottom which is intended to print out the name that has been selected ( in the variable operator), but it's not printing anything out. Can anyone see any issues?
EDIT *** Here's the updated code after some advice from Barmar with the variable information also, for some reason the echo statement still isn't working:
<?php
$connection = pg_connect("host=10.100.51.42 port=5432 dbname=reportingdb user=rai_gui password=password");
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
$sql="SELECT * FROM radio_archive_index_gui.t_operator ORDER BY f_operator_id";
$result = pg_query($connection, $sql);
if (!$result){
die("Error in SQL query: " . pg_last_error());
}
while ($arr = pg_fetch_array($result, null, PGSQL_ASSOC)){
$operator_id=$arr['f_operator_id'];
$operator=$arr['f_operator_name'];
echo "<option value='$operator'>$operator</option>";
}
echo "</select>";
##### submit form to carry out echo statement for testing purposes
echo "<ul>
<th><input type='submit' name='new' value='Confirm Information'/></th>
</form>
</ul>";
if (isset($_POST['new']))
{
echo $_POST['f_operator_id'];
}
?>
You can't put <select> inside <h2> and then put the <option>s and </select> outside it. HTML elements have to be nested properly, and <option> has to be inside <select>.
Change it to:
echo "<h2>Operator:</h2> <select name='f_operator_id'>";
And the index in $_POST has to match the name of the <select>, so $_POST['operator'] should be $_POST['f_operator_id'].

mysql distinct to form selections

I have to create a form that takes the select options from mySQL database so that all different values in one column are listed. The code that is not working is following:
<form class="form-horizontal" method="get" action="startlist.php">
<select id="selectbasic" name="klass" class="form-control">
<?PHP
$connection = mysqli_connect("link","dbuser","pass","dbname");
mysqli_set_charset($connection,"utf8");
$sql = "SELECT DISTINCT Klass FROM Voistlejad";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_array($connection,$result)) {
echo $row["Klass"];
echo "<option value='".$row[0]."'>".$row[0]."</option>";
}
mysqli_close($connection);
?>
<option value='32KK5B'>32KK5B</option><!--This is how it needs to be-->
</select>
<button type='submit' class="btn btn-primary">Move on</button>
</form>
What is wrong?
I think you are mixing up where to use single and double quotes..
Generally, for arrays/objects like $row, you get the value from the array with either no quotes, or single quotes, echo $row['Klass']
while ($row = mysqli_fetch_array($connection,$result)) {
echo $row['Klass']; // single quotes
echo "<option value='". $row['Klass'] ."'>". $row['Klass'] ."</option>";
}
// shows sql error ONLY if there is one to show
printf(" %s\n", mysqli_error($link));
P.S strongly recommend adding code to display MySQLi errors, because stuff happens!

How to store sql column name in form value

I'm new to php and MySQL (working through "Head First: PHP & MySQL"). I'm trying to grab a MySQL table column name from an array and store it in the "value" for an input field of a form. For some reason, I only end up with a blank page. When I simply type in some text into the 'value', the page turns out fine.
I've checked every line of code and have narrowed it down to the input value. The database connection works and I can echo the column name in the 'while' loop, but not in the 'value'.
Here's the code:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> ">
<?php
$dbc= mysqli_connect('host', 'user', 'pass', 'elvis_store')
or die ('Error connecting to the database');
$query= "SELECT * FROM elvis_emails";
$result= mysqli_query($dbc, $query)
or die ('Error querying database');
while ($row= mysqli_fetch_array($result)){
echo '<input type="checkbox" value="'$row['Id']'" name="todelete[]" />';
echo $row['Id'];
echo ' ' . $row['first_name'];
echo ' ' . $row['last_name'];
echo ' ' . $row['email'];
echo '<br />';
}
mysqli_close($dbc);
?>
<input type="submit" name="submit" value="Remove" />
</form>
Thank you!
instead of,
echo '<input type="checkbox" value="'$row['Id']'" name="todelete[]" />';
try like this
echo '<input type="checkbox" value="'.$row["Id"].'" name="todelete[]" />';
What is really breaking the code is the missing of proper concatenation.
It's good practice to avoid printing HTML elements with PHP echo statement. Rather, the best approach in this case would be:
<?php while ($row = mysqli_fetch_array($result)){ ?>
<input type="checkbox" value="<?=$row['Id']?>" name="todelete[]" />
<?php
...
}
mysqli_close($dbc);
?>
If by anyways you do need to print a piece of code that is relying on HTML tags and PHP variables you can make use of HEREDOC instead of using echo to print multiple lines of code.
Also, make sure that you are naming the keys to the row array the
same as your query return values.

PHP dropdown not sending through POST

I have drop downs built by a PHP function pulling from MySQL tables. The drop downs work fine but the selected option isn't POSTing. I suspect the problem is with how the HTML select tag is created in PHP but I can't seem to find a way to correct it. I've searched around but nothing seems to work.
Here is the PHP function to build the drop down:
function contact_list(){
//Connection info
$dbhost = 'localhost';
$db = 'database';
$dbuser = 'user';
$dbpass = 'password';
//Define connection
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $db);
//Check connection
if (mysqli_connect_errno()) {
echo " ERROR " . mysqli_connect_error() . " ERROR ";
}
//Define search strings for drop down
$contact_list = "SELECT * FROM CONTACT_LIST";
//Execute search of primary_contact table
if (!mysqli_query($con,$contact_list)) {
die('' . mysqli_error($con));
}
//Return primary_contact results as string
$contact_result = $con->query($contact_list);
//Build drop down
echo "<select name='Primary_Contact'>";
//Loop through results
foreach ($contact_result as $row)
{
echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";
}
echo "</select><p></p>";
}
Here is a sample of the HTML. The Description text box is fine so I don't think the problem is with the form tags.
<form action="mod_output_test.php" method="POST">
<u>Description</u>: <p></p>
<input type="text" name="Description" size="48" value="<?=$Description;?>"> <p></p>
<!-- Primary Contact -->
<?php echo "<u>Primary Contact</u>:<p></p>Current value:&nbsp<b> " . $Primary_Contact . "</b>&nbsp New value: ";?>
<?php contact_list() ?>
<p></p>
<input type="submit">
</form>
Change
echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";
to
echo "<option value='".$row['Id']."'>" .htmlspecialchars($row['Primary_Contact']). "</option>";
And I am guessing your dropdows is between a form tag.
in the top of the action file type this and tell us what you get
<?php
print_r($_REQUEST);
exit;
?>
echo "<option value='' >" .htmlspecialchars($row['Primary_Contact']). "</option>";
When you post like that, option value is empty. Add the id of the contact.
As they said, we see no <form> tag, we "assume" you add it before that piece. But the answer to "is not posting" is because value from <option> is empty.

Fill drop down list on page load php

I have two input text fields where user has to specify the begin and end of the fly.
<input type="text" name="start" placeholder="Start destination">
<input type="text" name="end" placeholder="End destination">
I would like to change that and give user to chose start and end destination from database.
<select>
<option value="$id">$name</option>
</select>
I know how to get done if i read database and input values manually, but i know its posible if page loads and execute my SELECT QUERY.
So i have to create dropdown list and fill that with a values from database.
This dropdown list has to be filled when the page load.
Some idea for this ???
I am working with php.
Thank you in advance !!
EDIT : I get done this only with php.
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "flights";
$conn = mysql_connect("$db_host","$db_username","$db_password") or die ("no conn");
#mysql_select_db("$db_name") or die ("no database");
if ($conn = true) {
// echo "";
}
//cyrilic
$sql = "SET NAMES 'utf8'";
mysql_query($sql);
//query for end
$sql="SELECT Distinct end from flights_table;";
$result=mysql_query($sql);
echo "<select name=\"city\">";
echo "<option>end destination</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['end']."'>".$row['end']." </option>";
}
echo "</select>";
?>
This php fires when page loads. Those select options i have putted in a form, and when form is submited, it fires php itself. I am getting selected options this way :
$startfly=$_POST['end'];
I am doing this for starting the flight :)
Thank you guys !
Try this :
At the top of page include your database connection file :
<?php
require "connection.php";
?>
Then :
<?php
$selectStart = "Start : <select name='start'>";
$selectEnd = "End : <select name='end'>";
$query = mysql_query("SELECT * FROM someTable ORDER BY dateField ASC");
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
$selectStart .= "<option value='".$row['startItem']."'>".$row['startItemName']."</option>";
$selectEnd .= "<option value='".$row['endItem']."'>".$row['endItemName']."</option>";
}
}
$selectStart = "</select>";
$selectEnd = "</select>";
?>
In your HTML :
<form action='destinationPage.php' method='post'>
<?php
echo $selectStart;
echo $selectEnd;
?>
<input type='submit' value='Submit' />
</form>

Categories