i think i am doing some mistake in my code. can someone please correct me. how to get the list box values in another page.
print "<form method=\"post\" action=\"fetch_test.php\">";
print "Report From: ";
print "<select name=fromdate[]>";
print "<option selected=\"selected\">Date</option>";
print "<option value=01>01</option>";
print "<option value=02>02</option>";
print "<option value=03>03</option>";
print "</select>";
print "<select name=frommonth[]>";
print "<option selected=\"selected\">Month</option>";
print "<option value=jan>Jan</option>";
print "<option value=feb>Feb</option>";
print "<option value=mar>Mar</option>";
print "</select>";
print "<br><br>";
print "<input type=submit>";
print "</form>";
fetch_test.php contains below code.
<?php
print $_POST['fromdate'];
print $_POST['frommonth'];
?>
You are using the name as an Array.
Fixed code:
print "<form method=\"post\" action=\"fetch_test.php\">";
print "Report From: ";
print "<select name=fromdate>";
print "<option selected=\"selected\">Date</option>";
print "<option value=01>01</option>";
print "<option value=02>02</option>";
print "<option value=03>03</option>";
print "</select>";
print "<select name=frommonth>";
print "<option selected=\"selected\">Month</option>";
print "<option value=jan>Jan</option>";
print "<option value=feb>Feb</option>";
print "<option value=mar>Mar</option>";
print "</select>";
print "<br><br>";
print "<input type=submit>";
print "</form>";
You stored it in an array, so call it in an array also.
<?php
print $_POST['fromdate'][$yournumbervalue];
print $_POST['frommonth'][$yournumbervalue];
?>
Try quoting your select tag names, you want to have <select name="fromdate[]"> to get an array, and you want to use <select from="fromdate"> to get a single value.
Unless there are more than one name='fromdate' or name='frommonth', you don't need the [].
Just try normal:
print "<select name='fromdate'>";
Make sure you put ' around the name attribute!
Then use:
$_POST['fromdate'];
At the moment you're putting it into an array, so if you want to keep it the way it is, use:
foreach ($fromdate as $date){
echo $date;
}
Related
The code that i made display the header after each row in the table.
I want just one time the header appear on the top.Any help?
print "<table border=1>\n";
while ($row = mysql_fetch_array($result)){
$files_field= $row['filename'];
$files_show= "Uploads/$files_field";
$descriptionvalue= $row['title'];
print "<tr>";
print "<th>";
echo "header1";
print "</th>";
print "<th>";
echo "header2";
print "</th>";
print "</tr>";
print "<tr>\n";
print "\t<td>\n";
echo "<font face=arial size=4/>$descriptionvalue</font>";
print "</td>\n";
print "\t<td>\n";
echo "<div align=center><a href='".$files_show."' target='_blank' title='CLICK TO OPEN'>$files_field</a></div>";
print "</td>\n";
print "</tr>\n";
}
print "</table>\n";
This program will:
get a row from db
print the header
print the data
get next row until the end
Sounds like you want to:
print the header
get a row from the db
print the data
get next row until the end
This should be adequate I think:
<?php
// print the beginning of the table, and the header
echo "<table border='1'>";
echo "<tr>";
echo "<th>";
echo "header1";
echo "</th>";
echo "<th>";
echo "header2";
echo "</th>";
echo "</tr>";
// than loop through the rows and print the rest of the table
while ($row = mysql_fetch_array($result)) {
$files_field = $row['filename'];
$files_show = "Uploads/{$files_field}";
$descriptionvalue = $row['title'];
echo "<tr>\n";
echo "\t<td>\n";
echo "<font face=arial size='4'/>$descriptionvalue</font>";
echo "</td>";
echo "<td>";
echo "<div align=center><a href='{$files_show}' target='_blank' title='CLICK TO OPEN'>{$files_field}</a></div>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
Some additional notes / advises:
don't mix print / echo: they are almost the same, it just causes confusion
don't try to "prettify" the source with tabs and linebreaks: you are just bloating the output for no good reason, use the browsers inspector window instead
forget the mysql driver, use mysqli: the former is so out of dae, that it is removed from the new versions of PHP (from 7.0), so your code will stop working if the platform is updated; the mysqli variant is a bit different in how you have to use it (an extra parameter mostly), but the results are in the same format, and it behaves very similarly if not identically to the old one, so you will not have to rewrite many things
In this code i am trying to fetch city names into the html dropdown. Kindly corrct me if i am wrong anywhere, it give me an error
<?php
$query = Run("select city_name from City");
echo "<select name="city-name" style="width: 210px;">";
while ($row = mssql_num_rows($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
?>
use mysqli_fetch_array($query) insted of mssql_num_rows($query)
try this one
echo "<select>";
while ($row =mysqli_fetch_array($query))
{
echo "<option value='".$row['city_name']."'>".$row['city_name']."</option>";
}
echo "</select>";
mssql_num_rows returns the number of rows in the result set, it doesn't iterate and return individual rows. Try using mssql_fetch_object instead.
If you start a string with " then you have to escape all occurrences of " inside your string or use '.
For example:
echo "<select name=\"city-name\" style=\"width: 210px;\">";
or
echo '<select name="city-name" style="width: 210px;">';
so you don't accidently close the string.
Also like the others pointed out, you have to use
mysqli_fetch_array($query).
Use this
$query = Run("select city_name from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_fetch_object($query))
{
echo "<option>$row->city_name</option>";
}
echo "</select>";
Use This
$query = Run("select `city_name` from City");
echo "<select name='city-name' style='width: 210px;'>";
while ($row = mssql_num_rows($query))
{
echo "<option value='.$row->city_name.'>".$row->city_name."</option>";
}
echo "</select>";
I'm having this following piece of code which populates a dropdown box based on the country column in my database. This works perfectly fine.
echo "<select name='pob_country' id='pob_country' data-native-menu='false'>";
echo "<option>Country</option>";
while ($row_country = mysql_fetch_array($result_countries)) {
echo "<option value='".
$row_country['country'] ."'>".
$row_country['country'] .
"</option>";
}
echo "</select>";
Now I want to set one <option> to selected based on a variable.
I tried it with a tenary operator, like so:
echo "<select name='pob_country' id='pob_country' data-native-menu='false'>";
echo "<option>Country</option>";
while ($row_country = mysql_fetch_array($result_countries)) {
echo "<option value='".
$row_country['country'] ."'".
(($pob_country=="$row_country['country']") ? "selected" : "") .
">".
$row_country['country'] .
"</option>";
}
echo "</select>";
Somehow this doesn't work, the page doesn't load.
I don't understand what I'm doing wrong here.
It probably is something really simple but I'm stuck on it for over an hour.
Any help is appreciated.
Remove the quotes:
($pob_country==$row_country['country'])
I'm trying to make a form in PHP that then passes the variables to the next page. My problem is that the drop down elements in my form aren't passing their values at all. When I do print_r($_GET) on the next page to see what values were passed through the form, only the inputs with type "text" or "radio" appear. I'm not sure what I'm doing wrong, as I've gotten this to work in the past. Can someone help me? I know that the variable names I'm pulling out of my MySQL queries are correct, as are the ids.
//begin form
print "<form action = 'restaurant_confirm.php' method = 'GET'>";
print "Restaurant Name: <input type = 'text' size = 50 name = restaurant_name></br>";
// drop down for cuisine
print "Cuisine: <select>";
while ($row = mysql_fetch_array($cuisine_result) ){
print "<option name = 'cuisine' value = '".$row['cuisine_id']."'>". $row['cuisine_name']. "</option>";
}
print "</select></br>";
// drop down form restaurant type
print "Restaurant Type: <select>";
while ($row = mysql_fetch_array($type_result) ){
print "<option name = 'restaurant_type' value = '".$row['type_id']."'>";
print $row['restaurant_type'];
print "</option>";
}
print "</select></br>";
//Check boxes for price point
//table for alignment
print "Select one price point </br>";
print "<table>";
//header row
print "<tr>";
print "<td>Price Point</td>";
print "<td>Value</td>";
print "<td>Select</td>";
print "</tr>";
while ($row = mysql_fetch_array($price_result)) {
print "<tr>";
//prints the price point
print "<td>";
print $row['price_point'];
print "</td>";
//prints the value
print "<td>";
print $row['price_value'];
print "</td>";
print "<td>";
// radio button to choose
print "<input type = 'radio' name = 'price_selection' value = ".$row['price_id'].">";
print "</tr>";
}
print"</table></br>";
//Check boxes for gluten type
//table for alignment
print "Select one gluten type</br>";
print "<table>";
//header row
print "<tr>";
print "<td>Gluten Type</td>";
print "<td>Select</td>";
print "</tr>";
while ($row = mysql_fetch_array($gluten_result)) {
print "<tr>";
//prints the gluten type
print "<td>";
print $row['gluten_type'];
print "</td>";
print "<td>";
// radio button to choose
print "<input type = 'radio' name = 'gluten_selection' value = ".$row['gluten_type_id'].">";
print"</td>";
print "</tr>";
}
print "</table></br>";
//submit button
print "<input type = 'submit' value = 'Add'>";
print "";
Your select has no name, which is needed.
Change the selects to <select name="whatever"> and you will be able to get them via $_GET["whatever"] on the next page.
Your input and select fields do not have name parameter specified. name defines the variable name.
So <input name="blah" /> will be $_GET['blah']
I'm having a bit of trouble using multiple queries within one php file. The code that I am trying to create would ideally create multiple html drop down menus populated with data from separate tables. My code is as follows:
echo "<form enctype='multipart/form-data' method='post' action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
$Wrap= #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Wrapping Paper: \n";
print "<Select name=\"Wrap_ID\">\n";
while ($row2=mysql_fetch_assoc($Wrap)){
$Wrap_ID=$row2['Wrap_ID'];
$Wrap_Picture =$row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture \n";
}
$result= #mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID = $myuserid");
$Wrap= #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Friend: \n";
print "<Select name=\"Friend_ID\">\n";
while ($row=mysql_fetch_assoc($result)){
$Friend_ID=$row['Friend_ID'];
$Name =$row['Name'];
print "<option value=$Friend_ID>$Name \n";
}
However, as it is now, this code will create a selection box for only one set of data. It does not matter which query I do first, it will either display a drop down menu for a friend's list, or a drop down menu for my wrapping paper, but it will not display both.
Any help would be appreciated.
You have forgotten ending <select>, <p>, <option> tag.
It should looks like:
<?php
echo "<form enctype='multipart/form-data' method='post' action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
$Wrap = #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Wrapping Paper:</p> \n";
print "<Select name=\"Wrap_ID\">\n";
while($row2 = mysql_fetch_assoc($Wrap)) {
$Wrap_ID = $row2['Wrap_ID'];
$Wrap_Picture = $row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture </option>\n";
}
echo '</select>';
$result = #mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID = $myuserid");
$Wrap = #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Friend: </p>\n";
print "<Select name=\"Friend_ID\">\n";
while($row = mysql_fetch_assoc($result)) {
$Friend_ID = $row['Friend_ID'];
$Name = $row['Name'];
print "<option value=$Friend_ID>$Name </option>\n";
}
echo '</select>';
After each of your while loops you need to end the <select> tag:
print "<select name=\"Wrap_ID\">\n";
while ($row2=mysql_fetch_assoc($Wrap)){
$Wrap_ID=$row2['Wrap_ID'];
$Wrap_Picture =$row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture</option> \n";
}
print '</select>';
You should also end your <option> tags properly like in the example above
You don't close your <selects>. The second set of data is there, but HTML ignores it. try viewing the source of your file you'll see.
You forgot to close your select's and your option's:
print "<p> Select a Wrapping Paper: \n";
print "<Select name=\"Wrap_ID\">\n";
while ($row2=mysql_fetch_assoc($Wrap)){
$Wrap_ID=$row2['Wrap_ID'];
$Wrap_Picture =$row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture</option>\n";
}
print "</select>"; //<-------add this
EDIT:
Also your paragraph. HTML requires tags to be closed:
<tag>content</tag>
So you would have:
print "<p> Select a Wrapping Paper: </p> \n";
instead of:
print "<p> Select a Wrapping Paper: \n";
Your full code should be:
echo "<form enctype='multipart/form-data' method='post' action='http://www.vgiver.com/uploadpicture1.php' name='Giftgiver'>";
$Wrap= #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Wrapping Paper:</p> \n";
print "<Select name=\"Wrap_ID\">\n";
while ($row2=mysql_fetch_assoc($Wrap)){
$Wrap_ID=$row2['Wrap_ID'];
$Wrap_Picture =$row2['Picture_Link'];
print "<option value=$Wrap_ID>$Wrap_Picture</option> \n";
}
print "</select>";
$result= #mysql_query("select Friend_ID, tbl_Friends.Name from tbl_Friends inner join tbl_Users on tbl_Friends.Access_ID=tbl_Users.Access_ID where tbl_Friends.Access_ID = $myuserid");
$Wrap= #mysql_query("select Wrap_ID, Picture_Link from tbl_Wrap");
print "<p> Select a Friend: </p> \n";
print "<Select name=\"Friend_ID\">\n";
while ($row=mysql_fetch_assoc($result)){
$Friend_ID=$row['Friend_ID'];
$Name =$row['Name'];
print "<option value=$Friend_ID>$Name</option> \n";
}
print "</select>";
Bonus hints:
Use single quotes instead of double quotes when you don't need to do parsing for variables as it is faster.
Use echo instead of print.