Page 1 of 1
Newbie: post selected value
Posted: Mon Mar 24, 2008 7:39 pm
by Dakke
How should one post a selected value from a php dropdown box? The value selection is on page 1, the php script doing the post on another.
Using Javascript or is there another way to do it?
Might seem a silly question, but very new to it all.
Re: Newbie: post selected value
Posted: Mon Mar 24, 2008 7:47 pm
by micknc
The form would look like this:
Code: Select all
<form name="form" method="post" action="step2.php">
<Select NAME="field">
<Option VALUE="one">One</option>
<Option VALUE="two">Two</option>
</Select>
<input type="submit" name="submit" value="Submit" />
</form>
In Step2.php you would grab the variables like this:
From there you can insert or echo or whatever you are looking for.
Here is a good tutorial:
http://www.tizag.com/phpT/forms.php
Re: Newbie: post selected value
Posted: Mon Mar 24, 2008 8:12 pm
by Dakke
And what if the values are done by a php script? Not using options but...
the DropdownSeries.php
Code: Select all
<?php
// Open DB
include("library/config.php");
include("library/opendb.php");
// Populate Drop Down
$query = "select series_english From series";
$results = mysql_query($query, $conn) or die("Error performing query");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->ID\">$row->series_english</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>
the form where it's all about
Code: Select all
<td>Series in English:
<select name="series_english">
<?php include('php/DropdownSeries.php'); ?>
</select>
Thanks for the reply, but for some reason it did not work.
Re: Newbie: post selected value
Posted: Mon Mar 24, 2008 8:24 pm
by dhampson
Dakke wrote:And what if the values are done by a php script? Not using options but...
the DropdownSeries.php
At first I noticed that there was no <select> tag, then I noticed that there was no <form></form> either. Having those would help to figure out the source of the problem.
After submitting a form, you can access your variables from the global $_POST[] or $_GET[] depending on the method you chose in <form>. The array name you are looking for is the id of the <select id="dropDownValue">.
I hope this helps.
Re: Newbie: post selected value
Posted: Mon Mar 24, 2008 8:26 pm
by micknc
You also have a syntax problem with the following line:
Code: Select all
echo("<option value="$row->ID">$row->series_english</option>");
Change to:
Code: Select all
echo("<option value='$row->ID'>$row->series_english</option>");
Re: Newbie: post selected value
Posted: Mon Mar 24, 2008 8:38 pm
by Dakke
Sorry, I posted only parts of it. The form tags are there, it's part of a larger form.
The tags of the form:
Code: Select all
<form action="InsertImage.php" method="post" enctype="multipart/form-data" name="uploadform">
</form>
Now the form looks like (again, part of it, others are simple text fields, which work:
Code: Select all
<form action="InsertImage.php" method="post" enctype="multipart/form-data" name="uploadform">
Series in English:
<select name="series_english">
<?php include('php/DropdownSeries.php'); ?>
</select>
</form>
DropdownSeries.php:
Code: Select all
<?php
// Open DB
include("library/config.php");
include("library/opendb.php");
// Populate Drop Down
$query = "select series_english From series";
$results = mysql_query($query, $conn) or die("Error performing query");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->ID\">$row->series_english</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>
Don't really understand what you meant by:
After submitting a form, you can access your variables from the global $_POST[] or $_GET[] depending on the method you chose in <form>. The array name you are looking for is the id of the <select id="dropDownValue">.
I changed the syntax problem.
Re: Newbie: post selected value
Posted: Tue Mar 25, 2008 10:45 am
by Jonah Bron
in "InsertImage.php":
Code: Select all
<?php
echo $_POST['series_english];//Or $_GET['series_english'] if form method is "get"
?>
Re: Newbie: post selected value
Posted: Tue Mar 25, 2008 3:23 pm
by Dakke
Well, it does something, in the sense that when I echo the selected drop down value, it returns:
\\\'\\\'
And I don't need to say that it ain't the value I choose.
Re: Newbie: post selected value
Posted: Tue Mar 25, 2008 4:21 pm
by dhampson
PHPyoungster wrote:in "InsertImage.php":
Code: Select all
<?php
echo $_POST['series_english];//Or $_GET['series_english'] if form method is "get"
?>
Oops, you forgot an apostrophe.
In insertImage.php, use: <?php echo $_POST['series_english']; ?>
Dakke, look at the source code being generated by your code. You select menu may look all right, but the values being "POST"ed may be way off.
--Dave
Re: Newbie: post selected value
Posted: Tue Mar 25, 2008 4:32 pm
by help-php
Just short note for newbies related to this:
"After submitting a form, you can access your variables from the global $_POST[] or $_GET[] depending on the method you chose in <form>."
You can use $_REQUEST[] to get dat from both submit methods (get, post).
From PHP manual:
Request variables: $_REQUEST
Note: Introduced in 4.1.0. There is no equivalent array in earlier versions.
Note: Prior to PHP 4.3.0, $_FILES information was also included in $_REQUEST.
An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.
http://ca.php.net/manual/en/reserved.variables.php
Re: Newbie: post selected value
Posted: Tue Mar 25, 2008 4:33 pm
by Dakke
I indeed spotted the apostrohe. And to get you and idea of it all, I'll post all the files for this code. Sorry, I'm new and learning every day a little more.
UploadImage.html
Code: Select all
<html>
<head>
<title>Upload File To MySQL Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.box {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
border: 1px solid #000000;
}
-->
</style>
</head>
<body>
<form action="php/InsertImage.php" method="post" enctype="multipart/form-data" name="uploadform">
<table width="800" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile" type="file" class="box" id="userfile"><br><br><br><br>
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "><br><br><br><br>
</td>
</tr>
<tr>
<td width="200">English Title: <input type="text" name="title_english" /><br><br>
</td>
<br>
<td width="500">Nederlandse Tittel: <input type="text" name="tittel_nederlands" /><br><br>
</td>
<br><br>
</tr>
<tr>
<td>Series in English:
<select name="series_english">
<?php include('php/DropdownSeries.php'); ?>
</select><br><br><br><br>
</td>
<td>Reeks in het Nederlands:
<select id="serie_id" name="serie_nederlands">
<?php include('php/DropdownSerie.php'); ?>
</select><br><br><br><br>
</td>
</tr>
<tr>
<td>Category in English:
<select name="categorie_nederlands">
<?php include('php/DropdownCategory.php'); ?>
</select><br><br><br><br>
</td>
<td>
Categorie in het Nederlands:
<select name="category_english">
<?php include('php/DropdownCategorie.php'); ?>
</select><br><br><br><br>
</td>
</tr>
<tr>
<td>English Description: <br><br> <input type="text" name="description_english" style="width:300px; height:150px;" /><br><br>
</td>
<td>
Nederlandse beschrijving: <br><br> <input type="text" name="beschrijving_nederlands" style="width:300px; height:150px;"/><br><br>
</td>
</tr>
</table>
</form>
</body>
</html>
InsertImage.php
Code: Select all
<?php
// Open DB
include("library/config.php");
include("library/opendb.php");
//Insert the image and create a unique name
// dir where to post the files
$uploadDir = './images/';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());
// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
include 'php/library/config.php';
include 'php/library/opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
}
// Insert text fields
$sql="INSERT INTO images (name, size, type, path, title_english, description_english, category_english, series_english, tittel_nederlands, beschrijving_nederlands, categorie_nederlands, serie_nederlands) VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$_POST[title_english]', '$_POST[description_english]', '$_POST[category_english]', '$_POST[series_english]', '$_POST[tittel_nederlands]', '$_POST[beschrijving_nederlands]', '$_POST[categorie_nederlands]', '$_POST[serie_nederlands]')";
if (!mysql_query($sql,$conn))
{
die('Error' . mysql_error());
}
echo "1 Image added";
echo $_POST['serie_nederlands'];
include("library/closedb.php");
?>
DropdownSeries.php
Code: Select all
<?php
// Open DB
include("library/config.php");
include("library/opendb.php");
// Populate Drop Down
$query = "select series_english From series";
$results = mysql_query($query, $conn) or die("Error performing query");
if(mysql_num_rows($results) > 0){
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->ID\">$row->series_english</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>