Click to See Complete Forum and Search --> : problem with multiple scripts/forms
tracy_jb
09-22-2003, 10:43 AM
Hope someone can point out something I am ignorant of...
I am trying to run two different query forms with seperate action scripts from the same web page... i.e.,
form1 action=./search1.php, form2 action=./search2.php
(not sure why) but only one or the other of these forms will display a results page... the other opens or tries to open the the "action script" rather than a results display... also it does not pass the variable (previously entered text) to the script ...
Am I missing something here?
thanks, tracy
Could you provide a little more detail? Are you getting any error messages? If so, what are they? Are you submitting these two forms on a different frame for each?
[J]ona
tracy_jb
09-22-2003, 06:16 PM
thanks for the reply,
I am not getting any errors both scripts/forms work fine... it is just that I can only get one to return query results when the search button is executed... if the other search button is executed it loads the the action script... which is php so the display is blank...
is there something I am missing about having two different forms with their own action scripts(php) on the same html doc?
There should be nothing wrong with it...
<form action="sub1.php" method="POST"><div>
<p><input type="text" name="q"></p>
<p><input type="submit"></p>
</div></form>
<form action="sub2.php" method="POST"><div>
<p><input type="text" name="p"></p>
<p><input type="submit"></p>
</div></form>
Do you have this page online?
[J]ona
tracy_jb
09-23-2003, 07:24 AM
As I said there is nothing wrong with the forms
the problem is only one or the other will return results the other just opens the script
I can play with the page and get one or the other to display results I can't even tell what causes the change from one to the other...?
the forms work fine and if either of the scripts is executed by itself there is no problem...i.e., if I set the either form on a page by itself there is no problem what so ever but as soon as I put both forms on the same page only one will return results the other just opens the script (if I include the form(imbeded) in the script it opens to the blank form rather than displaying the results AND it doe not pass the variable to the new form?)
TIA for any insights
As Jona asked, is the form online somewhere, so we can look at it?
tracy_jb
09-23-2003, 02:10 PM
I could put the forms online but at this time the database is not on a public server.
below is the page with both forms, followed by the action scripts... I can take either form/script and put it on a page of its own and they both work
----------------------------------------------------------------------
<body>
<table width="98%" border="0" cellpadding="0" cellspacing="0">
<tr valign="top">
<td width="34%" height="67" rowspan="2">
<h2> </h2>
</td>
<td width="29%" height="67" rowspan="2">
<table width="223" height="65" border="0" cellpadding="0" cellspacing="0" align="center">
<tr>
<td width="223" height="63" valign="top">
<form action=searchform2.php method=post>
<font size="4"><b> Search a City &<br>
Business Category: </b></font>
<table width="214">
<tbody>
<tr>
<td valign="top">
<select name=city>
<option value="" selected>New York
<option>Los Angeles
<option>Miami
</select>
<br>
<select name=category>
<option selected>Television
<option>Print
<option>Radio
<option>www
</select>
</td>
</tr>
</tbody>
</table>
<input type=submit value=search name=submit>
</form>
</table>
</td>
<td width="19%" height="3">
<div align="center">
<p><font size="2"><br>
<script language="javascript" type="text/javascript">
function checkInput()
// verify that the user input some data before submission
{
var searchTerm = document.forms.searchForm.searchTerm.value;
if (searchTerm == "")
{
alert("You did not enter a name to search for, please try again.");
return false;
} else {
return true;
}
}
</script>
</font></p>
</div>
</td>
<td width="18%" height="67" rowspan="2">
<div align="center"><font size="2"><br>
<br>
<br>
</font></div>
</td>
</tr>
<tr valign="top">
<td width="19%" height="105">
<div align="center">
<form name="searchForm" action="searchform1.php" method="post">
<font size="4"><strong>Search by Name</strong></font>:
<input type="text" name="searchTerm" size="25" maxlength="45">
<input name="submit2" type="submit" value="Search">
</form>
----------------------------------------------------------------------//searchform2.php
//this can be used as a stand alone application
<body>
<p><a href="javascript:history.go(-1);">Go back</a></p>
<p>
<?php
/*
broke this into functions...much easier to see and follow the flow of the program
*/
/* main code block */
//see if the form is submitted
if (!$_POST['submit']){
show_form();
}else{
//form submitted and need to get record(s)
get_data();
}//end if
//end main code block
function show_form()
{
//assume that city and category come from database
(connection data removed for space)
//get data for city and category
$sql = 'SELECT DISTINCT city FROM bus_list ORDER BY city';
$r = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($r))
{
$city[] = $row->city;
}
$sql = 'SELECT DISTINCT category FROM bus_list';
$r = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($r))
{
$category[] = $row->category;
}
$result_city = mysql_query($sql) or die ("Can't connect to perform query one because ".mysql_error());
$result_category = mysql_query($sql) or die ("Can't connect to perform query two because ".mysql_error());
if ((!result_city)||(!$result_category)){
echo "error retrieving data";
}else{
//place the results into an array
while ($rows=mysql_fetch_array($result_city )){
$city[]=$rows['city'];
}
//while ($rows1=mysql_fetch_array($result_category)){
//$category[]=$rows1['category'];
//}
//show the form headers
echo "
<body>
<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">
<table>
<tr>
<td>Select city</td></td>
<td>Select category</td>
</tr>
<tr>
<td><select name=\"city\">";
//show the city data
for ($x=0;$x<count($city);$x++){
echo "<option value=\"".$city[$x]."\">".$city[$x];
}
echo "</select></td><td>
<select name=\"category\">";
//add the category data
for ($x=0;$x<count($category);$x++){
echo "<option value=\"".$category[$x]."\">".$category[$x];
}
echo "</select></td></tr></table>
<input type=\"submit\" name=\"submit\" value=\"search\">
</form></body>";
}
}//end function show_form
function get_data()
{
//get data from form
$city=$_POST['city'];
$category=$_POST['category'];
$sql="select * from bus_list where city='$city' and category='$category' ORDER BY name";
mysql_connect("localhost", "#######", "#######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
$result=mysql_query($sql) or die ("can;t query because ".mysql_error());
echo "<div align=\"center\">\n";
echo "<table border=\"0\" cellspacing=\"6\" cellpadding=\"0\" width=\"90%\" >" ;
$a = 2;
while ($row=mysql_fetch_array($result)) {
if ($a%2 == 0) {
$bg = "#FFFFFF";
} else {
$bg = "#99FFCC";
}
echo"<tr bgcolor=\"".$bg."\"><td><img src=favicon.ico border=0></td><td>".$row['name']."</td><td>".$row['address']."</td><td>".$row['city']."</td><td>".$row['phone']."</td><td>".$row['www']."</td><td>";
$a++;
}
echo "</table>";
//show results to user
}
//end function get_data
?>
--------------------------------------------------------------------
searchform1 to follow in next msg.
tracy_jb
09-23-2003, 02:11 PM
//searchform1
connect data removed for space
// initialize some vars
$PHP_SELF = $_SERVER['PHP_SELF'];
$searchTerm = trim($_POST['searchTerm']);
$submit = trim($_POST['submit']);
// the form, customize to your taste
$input_form = <<< END
<form name="searchForm" action="searchform.php" method="post" onSubmit="return checkInput();">
Search by name:
<input type="text" name="searchTerm" size="25" maxlength="45">
<input name="submit" type="submit" value="Search">
</form>
END;
// print a nice error message, customize to your taste
$error_msg = <<< END
<font color="red">We are unable to process your request at this time, please try again in a few moments.</font>
<br><br>
$input_form
END;
// if no results, customize to your taste
$no_results = <<< END
There were no results with the searchterm '<font color="blue">{$searchTerm}</font>', please try again.
<br><br>
$input_form
END;
<script language="javascript" type="text/javascript">
function checkInput()
// verify that the user input some data before submission
{
var searchTerm = document.forms.searchForm.searchTerm.value;
if (searchTerm == "")
{
alert("You did not enter a name to search for, please try again.");
return false;
} else {
return true;
}
}
</script>
</head>
<body onLoad="document.forms.searchForm.searchTerm.focus();">
<?
// begin main script
// start by checking to see if the form has been submitted and
// validate the entry terms
if ((!empty($submit)) && ($submit == "Search"))
{
if (empty($searchTerm))
{ // if for some reason our javascript check failed
die("You did not enter a name to search for, please try again.<br>\n".$input_form);
}
else
{
if (!$link = @mysql_connect($mysql_server, $mysql_user, $mysql_pass))
// if cannot connect to server, kill the script with an error
die($error_msg);
if (!$select_db = @mysql_select_db($mysql_db))
// if cannot connect to database, kill the script with an error
die($error_msg);
$sql = "SELECT * FROM bus_list WHERE name LIKE '%".$searchTerm."%' ORDER BY name ASC LIMIT 0,30";
if (!$result = @mysql_query($sql))
// if the result didn't work, kill the script with an error
die($error_msg);
// count the result rows, do something with the value
$mysql_num_rows = mysql_num_rows($result);
switch($mysql_num_rows)
{
case 0:
die($no_results);
break;
case 1:
$results_str = "Search returned 1 result from the database:<br>\n";
break;
default:
$results_str = "Search returned $mysql_num_rows results from the database:<br>\n";
}
// if there is a result, print it out in a table
echo "<div align=\"center\">\n";
echo $results_str;
echo "<table border=1 cellpadding=2 cellspacing=0 width=\"90%\">\n";
echo "<tr>\n";
echo "<td class='title'>name</td>\n";
echo "<td class='title'>address</td>\n";
echo "<td class='title'>city</td>\n";
echo "<td class='title'>zip</td>\n";
echo "<td class='title'>phone</td>\n";
echo "<td class='title'>www</td>\n";
echo "</tr>\n";
$alt_color = 0;
while($row = @mysql_fetch_array($result))
{
extract($row); // grabs the array and splits into vars
// alternating row colors
$alt_color++;
$rowcolor = ($alt_color % 2) ? "#dcdcdc" : "#ffffff";
echo "<tr bgcolor=$rowcolor>\n";
// end alternating row colors code
echo "<td>$name</td>\n";
echo "<td>$address</td>\n";
echo "<td>$city</td>\n";
echo "<td>$zip</td>\n";
echo "<td>$phone</td>\n";
echo "<td><a href=\"$www\" target=\"_blank\">$www</a></td>\n";
echo "</tr>\n";
}
echo "</table>\n";
echo "</div>\n<br><br>\n";
echo $input_form."<br>\n";
} // close the second if-else statement
} else {
// no submission, print the regular form
echo $input_form;
} // close the first if-else statement
?>