Jump to content

Recommended Posts

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

"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?

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

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.

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>";

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

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!

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'];
}
  
  ?>

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 ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.