PDA

View Full Version : problem loading data on first click


davehaz
06-07-2006, 04:09 PM
howdy, I have written a php/javascript script that I am having a problem with, don't know whether to post it here or in the js forum. This script works but pulls the data one click late. Let me know if you see what is wrong here.
let me know if I should post in the Javascript forum.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Your Shoe Size & Dimensions</title>
<script type="text/javascript" src="../scripts/sizer.js"></script>
</head>
<?php
$dbh=MySQL_connect ("localhost", "user", "pwd")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("db");

$query = "SELECT * FROM womens ORDER BY uid ASC";
$result = mysql_query($query);
echo"<form method='POST'>";
echo "<table>";
echo "<tr><td><select name='size' >";
while($data=mysql_fetch_array($result)){
echo "<option value='".$data['0']."' >".$data['0']."</option>";
}
?>
<body>
</td><td><input name='submit' type='submit' value='Select Size' onClick="
<?
$size = $_POST['size'];
$select = "SELECT * FROM womens WHERE size = '$size'";
$result=mysql_query($select);
while($info=mysql_fetch_array($result)){
$size1=$info['0'];
$length=$info['1'];
$nwd=$info['2'];
$mwd=$info['3'];
$wwd=$info['4'];
}
?>
sizer('<? echo $size1 ?>','<? echo $length ?>','<? echo $nwd ?>','<? echo $mwd ?>','<? echo $wwd ?>','#ffffcc','')" >
</td></tr></select></table></form>
</body>
</html>


what happens here is after you select the shoe size and click the submit button you get a pop up but it is not populated with the data from the db. if you select another shoe size and click submit again you get the data from the previous size selected.

tia

raf
06-08-2006, 09:05 AM
i'd suggest cleaning up your code since the <body> is sent inside a tablecell and your formtag doesn't contain an action attribute + the while-loop after the size select doesn't make any sense at all + the dropping in and out of php-mode like that makes your code hard to read.

change

$select = "SELECT * FROM womens WHERE size = '$size'";
$result=mysql_query($select);

into

$select = "SELECT * FROM womens WHERE size = '$size'";
echo $select;
$result=mysql_query($select);


and then check the printed query. On the first submit, you'll probaly get ...WHERE size =''

i think that the problem is that you populate the sizer() when you load the page, with the size-details from the last posted size. Which isn't what you want because there is no point in putting an onclick event on a submitbutton where you then post preset sizedetails --> the onclick should then better be placed on the dropdown with the selected value as the atribute-values.
Normally, your onclick (on the dropdown) will
- open a new window
- load the page that selects the sizedetails
- and the selected size is added to the querystring of the loaded page