Computing.Net > Forums > Web Development > PHP/HTML form troubles

PHP/HTML form troubles

Reply to Message Icon

Original Message
Name: ShaqDiesel
Date: May 20, 2008 at 13:16:40 Pacific
Subject: PHP/HTML form troubles
OS: winXP
CPU/Ram: amd64/1Gb
Model/Manufacturer: custom
Comment:

Hello everyone, I am running into a few small problems with my php form.

1.Why don't my images show? Instead I get broken image icons.
while($result_row=$result->fetchRow())
{
echo "<tr><td>";
echo "" . "</td><tr>";
}

2.I have a pulldown menu. When the form is submitted, the correct results are shown but the menu isn't updated with my selection:

$category = htmlentities($_GET["pulldown"]);
$self = htmlentities($_SERVER['PHP_SELF']);

echo('
<form action="'.$self.'" method="get">
<label>Search: </label>
<select name="pulldown">
<option value="all">All
<option value="econ">Econ
<option value="bio">Biology
</select>
<input type="text" name="search" size="100"/>
<input type="submit" value="Show Results" />
</form>
');
query_db($category, $search);

By the way, why do I need to surround $self with single quotes as a value for the form's ACTION attribute?

Thanks in advance.


Report Offensive Message For Removal


Response Number 1
Name: ShaqDiesel
Date: May 20, 2008 at 13:18:11 Pacific
Reply: (edit)

Aargh, this forum is blocking my img code that should go in between the two lines of my while loop body. How do I post it properly?

echo "img src=\"" . $result_row[0] . "\">" . "</td><td>";

anyway there is a "<" before the img


Report Offensive Follow Up For Removal

Response Number 2
Name: Michael J (by mjdamato)
Date: May 20, 2008 at 15:14:11 Pacific
Reply: (edit)

1. Well, what is the path that is written to the page for the images? Are the image paths what is in the database? Are they valid paths from the working directory?

2. If I understand you correctly you are POSTing to the same page and want the select list to automatically select the same value that was submitted.


$category = htmlentities($_GET["pulldown"]);
$self = htmlentities($_SERVER['PHP_SELF']);


$catArray = array('all','econ','bio');


echo "<form action=\"$self\" method=\"get\">\n";
echo " <label>Search: </label>\n";
echo " <select name=\"pulldown\">\n";


foreach ($catArray as $catValue) {
$selected = ($category==$catValue)?' selected="selected"':'';
echo " <option value=\"$catValue\">$catValue</option>\n";
}


echo " </select>\n";
echo " <input type=\"text\" name=\"search\" size=\"100\" />\n";
echo " <input type=\"submit\" value=\"Show Results\" />\n";
echo " </form>\n";


query_db($category, $search);

By the way, why do I need to surround $self with single quotes as a value for the form's ACTION attribute?

When specifying strings using single quote marks variables in the string are interpreted as literal text - i.e. $text will be treated as the string '$text'

If you use double quotes when specifying a string, then variables are parsed for their values: i.e. $text would be interpreted as it's saved value.

So in your exampe above you have to "escape" out of the quotes for the $self value to be interpreted.

Michael J


Report Offensive Follow Up For Removal

Response Number 3
Name: ShaqDiesel
Date: May 20, 2008 at 19:06:58 Pacific
Reply: (edit)

Can you explain what this line does?
$selected = ($category==$catValue)?' selected="selected"':'';

Since single quotes won't interpret $self, why does adding single quotes around $self work?

As for the images, I used the full path and it looks fine in phpmyadmin. However, when I view the image in the browser part of the pathname, the '/' is messed up.

UPDATE on the images: It only works if the image is in the same folder as my php script. I can't put an absolute or relative path to the image. I'm trying to figure out WHY because when I right click on the broken image icon and select "copy image location" and paste it into notepad I see the location is correct.


Report Offensive Follow Up For Removal

Response Number 4
Name: ShaqDiesel
Date: May 20, 2008 at 19:11:43 Pacific
Reply: (edit)

Where do you actually use $selected? I didn't find it in the code snippet you posted.


Report Offensive Follow Up For Removal

Response Number 5
Name: Michael J (by mjdamato)
Date: May 20, 2008 at 23:03:06 Pacific
Reply: (edit)

OK, one thing at a time:

"Can you explain what this line does?
$selected = ($category==$catValue)?' selected="selected"':'';"

That is called a ternary operator. Basically it is a sort of shorthand format of an if/else statement. In that particular line of code it sets the value of $selected based upon the condition of ($category==$catValue).

Example:

$result = ($a == $b) ? 'true' : 'false' ;

"Since single quotes won't interpret $self, why does adding single quotes around $self work?"

It doesn't. In the code you posted you had something like this:

echo '[a href="' . $url . '"]Link[/a]';

Inthat example, the first single quote starts a literal string. The next single quote ends the literal string (the double quote is treated as part of the string). After that second single quote any variables will be interpreted by their value. There is then another single quote to start a literal string and then a final single quote at the end of the line.

You could get the same exact result with this since variable are interpreted within strings defined with double quotes. but since there are suppiosed to be double quotes as part of the string you have to escape them with a \ otherwise the interpreted would think you were ending the string when the 2nd double quote was parsed.

echo "[a href=\"$url\"]Link[/a]";

"It only works if the image is in the same folder as my php script"

Sounds to me like the path is not correct from the working directory. You either need to specify the full path from the domain OR you can specify the relative path from the working directory. So, if the script you are running is in the path "htdocs/theform" and your images are in the folder "htdocs/images", you would specify the path as "../images/theimage.jpg". The ".." means to go up one level. I think you can also specify to start from the root folder using a single period. That might be easier for you. Just right-click the broken image and check the URL. I am sure it will show that the directory path to the image is not 100% correct.

"Where do you actually use $selected?"

My mistake. It should have been included in this line:

echo "    <option value=\"$catValue\"$selected>$catValue</option>\n";

Michael J


Report Offensive Follow Up For Removal


Response Number 6
Name: ShaqDiesel
Date: May 21, 2008 at 14:26:34 Pacific
Reply: (edit)

As it turns out I am supposed to use forward slashes instead of the backslash in my image file pathname. I used a backslash initially because that is the way it looks in the windows filesystem. Well, that solves all my problems thank you for your help.


Report Offensive Follow Up For Removal

Response Number 7
Name: neelchauhan
Date: May 24, 2008 at 08:54:29 Pacific
Reply: (edit)

Try This:

$category = $_GET["pulldown"];
$self = $_SERVER['PHP_SELF'];

Neel Chauhan


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PHP/HTML form troubles

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 6 Days.
Discuss in The Lounge