phpdragon Posted January 28, 2009 Share Posted January 28, 2009 I have a weird problem with php variables, not being defined in firefox, but being defined in internet explorer, i cant for the life of me see the issue. web link : www.lostpigeon.com.au/subscribe.php I have ajax working on a dive tag to dynamically update the state select box depending on what is selected from the country select box, this works no problems. The problem comes with reloading the page after clicking submit and one of the errors are activated on the process part of the page and cause page to reload listing errors. I get the select boxes to check if they were selected previously and then select the previous one as the default option using the echo command. This works fine for the first select box, but just does not work in fire fox for the second select box listing the state/province. I have attatched the code as a text file subscribe.txt, the site link operating is above and the section of code where the problem is below. <td><label for="country">Country: </label><select name="country" id="country" onchange="getState(this.value);"> <option value="--Please Select Country--">--Please Select Country--</option> <?php // print category combo box $sql = "SELECT name,ccode FROM countries ORDER BY name ASC"; $result = mysql_query($sql); while ($result_row = mysql_fetch_array($result)) { $cname = $result_row["name"]; $cid = $result_row["ccode"]; echo "<option value='$cid'"; if (($_POST['country']) == ($cid)) { echo "selected"; $c=$cid; $n=$cname; } echo ">$cname</option>\n"; } ?> </select></td> </tr> <tr> <td><label for="state"> State: </label><div id="statediv"><select name="state" id="state"> <option value="--Please Select State/Province--">--Please Select State/Province--</option> <?php ini_set('display_errors', 1);error_reporting(E_ALL); $sql2 = "SELECT * FROM states WHERE code='$c' ORDER BY nameorder"; $res = mysql_query($sql2); while ($row = mysql_fetch_array($res)) { $statename = $row["name"]; echo "<option value='$statename'"; if (($_POST['state']) == ($statename)) { $tt="selected"; echo "$tt>$statename</option>\n"; } else { echo ">$statename</option>\n"; } } if (!$stn) { echo "<option value='$n'>$n</option>\n"; } ?> </select></div></td> [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/142723-undefined-variables-in-firefox-but-defined-in-ie7-using-dynamic-select-boxes/ Share on other sites More sharing options...
genericnumber1 Posted January 28, 2009 Share Posted January 28, 2009 You are setting $c and $tt (please use more meaningful variable names in the future) inside of an if() statement, then referencing outside of the if statement. This is all well and good if the if statement evaluates to true, but when it evaluates to false, the variables are never set, when it comes to the point when you use them. Link to comment https://forums.phpfreaks.com/topic/142723-undefined-variables-in-firefox-but-defined-in-ie7-using-dynamic-select-boxes/#findComment-748136 Share on other sites More sharing options...
phpdragon Posted January 28, 2009 Author Share Posted January 28, 2009 Thank you for your reply and I have updated code as follows, but still same problem <td><label for="country">Country: </label><select name="country" id="country" onchange="getState(this.value);"> <option value="--Please Select Country--">--Please Select Country--</option> <?php // print category combo box $sql = "SELECT name,ccode FROM countries ORDER BY name ASC"; $result = mysql_query($sql); while ($result_row = mysql_fetch_array($result)) { $cname = $result_row["name"]; $cid = $result_row["ccode"]; echo "<option value='$cid'"; if (($_POST['country']) == ($cid)) { echo "selected"; $valueCid=$cid; $valueName=$cname; } echo ">$cname</option>\n"; } ?> </select></td> </tr> <tr> <td><label for="state"> State: </label><div id="statediv"><select name="state" id="state"> <option value="--Please Select State/Province--">--Please Select State/Province--</option> <?php ini_set('display_errors', 1);error_reporting(E_ALL); $sql2 = "SELECT * FROM states WHERE code='$valueCid' ORDER BY nameorder"; $res = mysql_query($sql2); while ($row = mysql_fetch_array($res)) { $statename = $row["name"]; echo "<option value='$statename'"; if (($_POST['state']) == ($statename)) { $lastSelect="selected"; echo "$lastSelect>$statename</option>\n"; } else { echo ">$statename</option>\n"; } } if (!$statename) { echo "<option value='$valueName'>$valueName</option>\n"; } ?> </select></div></td> Regarding the using the variable outside the if() statement, i only use the variable from country if () in the state if() and the state list is only visible if the country is selected equating to true anyway. I believe that is how i have the logic. Is there a better way to do this, the function is -Select Country-->populates state select box with country state/province values -Select State-->selects state value now if you hit subscribe and dont enter any more details, the page reloads stating you have missed the other information. -The function matches the posted country with the country in the array and marks it as selected in select box - Function sets country id for selected country for state search function -Select box for states populated by array defined by country id --The function matches the posted state with the state in the array and marks it as selected in select box country selected function works but the state selected function does not yet it populates the select box with the state names. note this only happens in firefox, both functions work in internet explorer, which is weird, as I thought they were server side and not browser issues, or is ther an issue with firefox and the select function of a select box? again if there is a better way to do this would be appreciated this has been a thorn in my side for a few days now. Link to comment https://forums.phpfreaks.com/topic/142723-undefined-variables-in-firefox-but-defined-in-ie7-using-dynamic-select-boxes/#findComment-748157 Share on other sites More sharing options...
phpdragon Posted January 28, 2009 Author Share Posted January 28, 2009 it does not seem to be activating the if statement in the state section to add the selected value, I cant figure out why this only occurs in firefox, any ideas? Link to comment https://forums.phpfreaks.com/topic/142723-undefined-variables-in-firefox-but-defined-in-ie7-using-dynamic-select-boxes/#findComment-748228 Share on other sites More sharing options...
dragon_sa Posted January 29, 2009 Share Posted January 29, 2009 It seems to explode the option tag in firefox after it inserts the variable for some reason, while I cannot figure out what is causing this, I have modified the code to look for a posted state and print that as the selected option outside the while loop. This works now in firefox but the only downside is it repeats the statename at the bottom of the list, this doest happen in internet explorer. no biggy as it selects what the user had previously anyway. I would love to know what is causing the code to blow out in fire fox if anyone has any ideas?? below is my subscribe page <?php session_start(); require('req/definitions.php'); include("data_connect.php"); $page = ('Subscribe'); // start error array $arrErrors = array(); //Only validate if the Submit button was clicked. if (!empty($_POST['submit'])) { // declare all variables $username =($_POST['name']); $useremail = ($_POST['email']); $usercountry = ($_POST['country']); $userstate = $_POST['state']; $usercode = $_POST['code']; $useradded = $_POST['added']; $validcode = $_SESSION['code']; // email and data input check $emailquery = "SELECT email FROM subscribers WHERE email='$useremail'"; $resultemail = mysql_query($emailquery); $selectemail = mysql_fetch_array($resultemail); $emailexist = $selectemail["email"]; if (!empty($emailexist)) $arrErrors['exist'] = $useremail . ' already exists'; if ($username=='') $arrErrors['name'] = 'Please enter a name'; if ($useremail=='') $arrErrors['email'] = 'Please enter valid email'; if ($usercountry=='--Please Select Country--') $arrErrors['country'] = 'Please select a country'; if ($userstate=='--Please Select State/Province--') $arrErrors['state'] = 'Please select a state'; if ($validcode!=$usercode) $arrErrors['code'] = 'Missing or Incorrect Code'; // convert country code to country name $querycnty = "SELECT name FROM countries WHERE ccode='$usercountry'"; $resultcnty = mysql_query($querycnty); $selectcnty = mysql_fetch_array($resultcnty); $namecnty = $selectcnty["name"]; // if no errors add to database if (count($arrErrors) == 0) { $sql = "INSERT INTO subscribers (name, email, country, state, added) VALUES ('" . $username . "', '" . $useremail . "', '" . $namecnty . "','" . $userstate . "', '" . $useradded . "')"; $result = mysql_query($sql); header("Location: thanks.php?thanks=subscribe&n=$username&e=$useremail"); } else { $strError = '<div class="formerrorf"><br/><p><img src="/images/triangle_error.gif" width="16" height="16" hspace="5" alt="Represents missing information"><b>Please check the following Errors:</b></p><ul>'; // Get each error and add it to the error string as a list item. foreach ($arrErrors as $error) { $strError .= "<li class='indent'><b>$error</b></li>"; } $strError .= '</ul></div>'; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo LOGO_ALT ?></title> <meta name="keywords" content="<?php echo KEYWORDS ?>" /> <link href="site.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" language="javascript" src="scripts/noEnter.js"></script> <script language="javaScript" type="text/javascript"> function getXMLHTTP() { //fuction to return the xml http object var xmlhttp=false; try{ xmlhttp=new XMLHttpRequest(); } catch(e) { try{ xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e1){ xmlhttp=false; } } } return xmlhttp; } function getState(codeId) { var strURL="statecall.php?countrycode="+codeId; var req = getXMLHTTP(); if (req) { req.onreadystatechange = function() { if (req.readyState == 4) { // only if "OK" if (req.status == 200) { document.getElementById('statediv').innerHTML=req.responseText; } else { alert("There was a problem while using XMLHTTP:\n" + req.statusText); } } } req.open("GET", strURL, true); req.send(null); } } </script> </head> <body> <table width="970px" border="0" cellspacing="0" cellpadding="5" align="center"> <tr> <td width="170px" align="center" valign="top"><br /> <?php $sql = "SELECT * FROM images WHERE name='subleft'"; $result = mysql_query($sql); $result_row = mysql_fetch_array($result); echo "<img src='site_images/" . $result_row["image"] . "' border='0' width='160' alt='" . LOGO_ALT . "'/>"; ?></td> <td rowspan="2" class="contact" width="450px" align="left" valign="middle"> <form name="subscribe" method="post" action="subscribe.php"> <table width="450" border="0" cellspacing="0" cellpadding="0"> <tr><td height="100px" class="contact" align="left" valign="middle"><b>Fill out the form below to join our mailing list.<br />Receive the latest goss and be the first to see the latest range.</b><br /></td></tr> <tr> <td><label for="name"> Your Name: </label><input name="name" type="text" size="35" id="name" value="<?php echo $username; ?>"/></td> </tr> <tr> <td><label for="email"> Email Address: </label><input name="email" type="text" size="35" id="email" value="<?php echo $useremail; ?>"/></td> </tr> <tr> <td><label for="country">Country: </label><select name="country" id="country" onchange="getState(this.value);"> <option value="--Please Select Country--">--Please Select Country--</option> <?php // print category combo box $sql = "SELECT name,ccode FROM countries ORDER BY name ASC"; $result = mysql_query($sql); while ($result_row = mysql_fetch_array($result)) { $cname = $result_row["name"]; $cid = $result_row["ccode"]; echo "<option value='$cid'"; if ($usercountry==$cid) { echo " selected='selected'"; $valueName==$cname; } echo ">$cname</option>\n"; } ?> </select></td> </tr> <tr> <td><label for="state"> State: </label><div id="statediv"><?php include('statecall.php'); ?></div></td> </tr> <tr> <td><input type="hidden" size="35" name="added" value="<?php echo date("d-m-Y H:i:s");?>"/></td> </tr> <tr> <td align="center"><br /> <label for="code">VERIFY CODE</label><img src="check/CaptchaSecurityImages.php" class="bw1" alt="VERIFY CODE" align="middle"/></td> </tr> <tr> <td align="center"><label for="submit"> </label><input name="code" type="text" id="code" size="6" maxlength="6" onkeypress="return noEnter(this, event)"/> <input name="submit" id="submit" type="submit" value="Subscribe"/></td> </tr> </table></form> </td> <td width="350" height="280" align="center" valign="middle" class="contact"><br /> <?php if (!empty($strError)) { echo $strError; } else { $sql = "SELECT * FROM images WHERE name='subright'"; $result = mysql_query($sql); $result_row = mysql_fetch_array($result); echo "<img src='site_images/" . $result_row["image"] . "' border='0' width='340' alt='" . LOGO_ALT . "'/>"; } ?></td> </tr> <tr> <td valign="bottom" align="center" class="pagetitle"><?php echo $page ?><br /><br /></td> <td width="350" align="center" valign="top"><iframe width="300px" height="120px" allowtransparency="true" name="go" frameborder="0" scrolling="no" src="none.html">This page uses iframes please upgrade your web browser, we apologise for any inconvenience.</iframe></td> </tr> <tr> <td height="50px" colspan="3" align="center" class="men"><?php include('menu_bottom.php'); ?></td> </tr> </table> </body> </html> and below is the statecall.php file to populate the state list <select name="state" id="state"> <option value='--Please Select State/Province--'>--Please Select State/Province--</option> <?php include('data_connect.php'); // print state select box // convert country code to country name if (!$countrycode) { $countrycode = $usercountry; } $getcnty = "SELECT * FROM countries WHERE ccode='$countrycode'"; $resultcnty = mysql_query($getcnty); $selectcnty = mysql_fetch_array($resultcnty); $cntyname = $selectcnty["name"]; $querystate = "SELECT * FROM states WHERE code='$countrycode' ORDER BY nameorder"; $result = mysql_query($querystate); while($row_result = mysql_fetch_array($result)) { $statename = $row_result["name"]; if ($statename) { if ($statename!=$userstate) { echo "<option value='$statename'>$statename</option>\n"; } } } if (!empty($userstate)) { echo "<option value='$userstate' selected='selected'>$userstate</option>\n"; } if (!$statename) { if (!empty($cntyname)) { echo "<option value='$cntyname' selected='selected'>$cntyname</option>\n"; } } ?> </select> Link to comment https://forums.phpfreaks.com/topic/142723-undefined-variables-in-firefox-but-defined-in-ie7-using-dynamic-select-boxes/#findComment-749147 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.