开发者

PHP search form

开发者 https://www.devze.com 2023-02-11 06:45 出处:网络
I am new to PHP so please forgive if this question or problem seem confusing I am trying create a search form that has a 2 dropdown option: St. Peter or JHS. the user types in the name of a professor

I am new to PHP so please forgive if this question or problem seem confusing I am trying create a search form that has a 2 dropdown option: St. Peter or JHS. the user types in the name of a professor and then choose the school the professor teach at. the form is has below

<form name=form action="form.php" method="GET">
<input type="text" name="find" /> 
<select name="school[]" >
<option value="">School</option>
<option value="1">St.Peter</option>
<option value="2">JHS</option>
</select>
<input type="submit" name="Submit" value="Search" />
</form>

the form.php script that i am using is

<?php
$q=$_GET["find"];
$s=$_GET["school"];
$con = mysql_connect('localhost', 'peter', 'abc123');

if($q && ($s==St.Peter))
echo "that professor belongs to St.peter H.S";
else{echo "that professor does not belong to this school"; }
elseif($q && ($s==JHS))
echo "that professor belongs to JHS H.S";
else{echo "that professor does not belong to this school"; }
?>

when i run this, the output i get is. that professor belongs to St.peter H.S that professor belongs to JHS H.S

if i change the script just to see if the first if statement is correct eg.

<?php
$q=$_GET["find"];
$s=$_GET["school"];
$con = mysql_connect('localhost', 'peter', 'abc123');

if($q && ($s==St.Peter))
echo "that professor belongs t开发者_StackOverflowo St.peter H.S";
else{echo "that professor does not belong to this school"; }

?>

this echos out that professor belongs to St.peter H.S, but if change

if($q && ($s==St.Peter))

to

if($q && ($s==St.P)) 

it echos out the same thing. this echos out that professor belongs to St.peter H.S,


if($q && ($s==St.Peter))

This is syntactically incorrect. PHP will see 'St.Peter' as a undefined constant, not a string, you should have

if($q && ($s == 'St.Peter'))

As well, your big if/else block is incorrect. You have

if (...) {
} else {
  ...
} else if {
  ...
}

which is a syntax error. an else is the final "if all else fails" in an if() series. you cannot follow it up with another else if. The block should be

if (...) {
} else if (...) {
} else if (...) }
} else {
}


You must compare with the value of the SELECT OPTION. Try:

if($q && ($s=='1'))


That's because what's actually being sent is the "value" of the option, not the text. You should be looking for "1" or "2"

if ($q && $s == 1)


Marc B. + contagious

<select name="school[]" >

This instructs PHP to create the GET['school'] parameter as an array. Useful for multiple checkboxes and such. You should go with

<select name="school" >
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号