unistake Posted August 20, 2009 Share Posted August 20, 2009 Hi all, I have a problem with the linking of some scripts. Basically I have a drop down list in a form on page form.php, when an option is clicked there is a onClick='showUser(this.name)'. This is sent to the function on the page selectoption.js. The javascript then takes the name value from the option and SHOULD send it to another PHP page called, getoption.php, then this option value is used to get data from a mysql DB and echo an output, that is incuded within a div tag in the initial form.php page! The whole script works, except the passing of the option name value 'showUser(this.name)' as as I try to echo the value it says "unverified". The scripts I have are... form.php <head> <script type="text/javascript" src="selectoption.js"></script> </head> <body> <?php include("login.inc"); $query = "SELECT * FROM database"; $result = mysqli_query($cxn,$query) or die ("Can't execute query!"); echo "<label> <select name='books' id='books'>"; while ($row = mysqli_fetch_assoc($result)) { extract($row); echo "<option value='$value' name='$value' onClick='showUser(this.name)'>$value</option>"; } echo "</select> </label>"; ?> <div id="txtHint"></div> selectoption.js var xmlhttp; function showUser(str) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="getoption.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } getoption.php <?php $q=$_GET["q"]; echo "$q"; ?> Thanks for you help and time. Tim Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/ Share on other sites More sharing options...
corbin Posted August 20, 2009 Share Posted August 20, 2009 "var xmlhttp; function showUser(str) { xmlhttp=GetXmlHttpObject();" You shouldn't use a global variable in that situation. You should do: function showUser(str) { var xmlhttp=GetXmlHttpObject(); Then change stateChanged to: function stateChanged() { if (this.readyState==4) { document.getElementById("txtHint").innerHTML=this.responseText; } } As for the PHP script, it sounds like the problem is in the script, not the JS. Have you tried going directly to the PHP page? If so, does it display the correct output? Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-902928 Share on other sites More sharing options...
unistake Posted August 20, 2009 Author Share Posted August 20, 2009 Hi Corbin, thanks for the reply. I have changed the JS as you said, and have tried going to the getoption.php directly, but there is no output at all, i am not sure what I was to expect when I went to it. Is a blank screen the output I was meant to see!? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-902935 Share on other sites More sharing options...
vineld Posted August 20, 2009 Share Posted August 20, 2009 If you set a value for q in the query string you should see this value. Remove the quotes from the variable in getoption.php: echo $q; Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-902945 Share on other sites More sharing options...
unistake Posted August 20, 2009 Author Share Posted August 20, 2009 Hi Vineld, In the form.php i changed the $value in this code <option value='$value' name='$value' onClick='showUser(this.name)'>$value</option>"; to a fixed field value in the mysql database and the output was again 'undefined'. I also removed the quotes as you said. Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-902952 Share on other sites More sharing options...
MadTechie Posted August 21, 2009 Share Posted August 21, 2009 What happens when you open getoption.php?q=TEST you SHOULD get a page that says TEST also <option value='$value' name='$value' onClick='showUser(this.name)'>$value</option>"; surely should be <option value='$value' name='$value' onClick='showUser(this.value)'>$value</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-902964 Share on other sites More sharing options...
unistake Posted August 21, 2009 Author Share Posted August 21, 2009 Hey MadTechie you are a legend! Thanks for the quick response and the eye for detail! Took me two goes to find the change you made in the code above! The time 01:14 is no excuse! Cheers Tim Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-902965 Share on other sites More sharing options...
unistake Posted August 21, 2009 Author Share Posted August 21, 2009 Whoops sorry! Topic re-opened! The $q was showing the correct output from the database, however i also want to echo more information from the database using this code in the getoption.php <?php $q= $_GET["q"]; echo $q; // <-------------- this output is shown //////////////////////////////////////// include("login.inc"); $sql="SELECT * FROM database WHERE column = '".$q."'"; $result = mysqli_query($sql); while($row = mysqli_fetch_array($result)) extract($row); { echo $details; } ?> any more suggestions? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-902977 Share on other sites More sharing options...
MadTechie Posted August 21, 2009 Share Posted August 21, 2009 change echo $details; to echo $row['FieldNames']; replace FieldNames with names of fields from the table also remove echo $q; Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-902987 Share on other sites More sharing options...
unistake Posted August 21, 2009 Author Share Posted August 21, 2009 I have replaced the echo $details with echo $row['FieldNames']; and also took out the extract($row) however the script is still showing a blank output. Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-903147 Share on other sites More sharing options...
unistake Posted August 21, 2009 Author Share Posted August 21, 2009 I have also changed the script of getoptions.php to <?php $q= $_GET["q"]; include("goatlogin.inc"); $sql="SELECT * FROM bookmakers WHERE bookmaker = $q" or die ("cant do \$sql"); $result = mysqli_query($cxn,$sql) or die ("Can't execute \$result!"); //// THIS IS WHAT IS SHOWN AS THE OUTPUT //// while($row = mysqli_fetch_array($result)) { echo $row['details']; } ?> The login.inc and the mysql table and fields are correct also, as I have double and triple checked them! Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-903156 Share on other sites More sharing options...
MadTechie Posted August 21, 2009 Share Posted August 21, 2009 Well you have changed quite a few other things try this <?php $q= $_GET["q"]; include("goatlogin.inc"); $sql="SELECT * FROM bookmakers WHERE bookmaker = '$q' "; //UPDATED you had missed quotes and an or die ? /*I assume you don't mean mysqli ad their is no mysqli_query() function, $cxn isn't set and even if it was you had it in the wrong place*/ $result = mysql_query($sql) or die ("Can't execute \$result!"); //// THIS IS WHAT IS SHOWN AS THE OUTPUT //// /*again i assume mysql_fetch_array instead of mysqli_fetch_array*/ while($row = mysql_fetch_array($result)) { echo $row['details']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-903201 Share on other sites More sharing options...
unistake Posted August 21, 2009 Author Share Posted August 21, 2009 Thanks MadTechie, sorry about the problem, I didnt realise that I posted an old script for that last one, I have solved the problem using, <?php $q=$_GET["q"]; include("login.inc"); $con = mysql_connect('localhost', 'user', 'password'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $sql="SELECT * FROM table WHERE column = '".$q."'"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { echo $row['details']; } ?> Thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-903287 Share on other sites More sharing options...
MadTechie Posted August 21, 2009 Share Posted August 21, 2009 Kool Can you click topic solved bottom left Quote Link to comment https://forums.phpfreaks.com/topic/171227-solved-php-included-files-and-_get-between-js-also/#findComment-903298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.