Jump to content

Update MySQL onClick with checkbox


hackerkts

Recommended Posts

Hi guys,

 

I'm quite new to ajax, so pardon me.

 

I have a html page where user will type in some unique number and it will search through MySQL and show their name. Beside their name there will be a checkbox for them to tick.

 

Here comes the problem, I'm trying to update my MySQL once user tick beside their name, ajax doesn't seems to work.

 

index.html

<html>
<head>
<script src="clienthint.js"></script>
</head>
<body>

<form>
Admin No: <input type="text" onKeyUp="showHint(this.value)" />
</form>
<p>Suggestions:</p> 
<p><span id="txtHint"></span></p>

</body>
</html>

 

clienthint.js

var xmlhttp

function showHint(str) {
if (str.length==0) {
	document.getElementById("txtHint").innerHTML="";
  		return;
  	}
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
	alert ("Your browser does not support XMLHTTP!");
  	return;
  	}
var url="gethint.php";
url=url+"?q="+str;
//url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function chkit(str) {
if (str.length==0) {
	document.getElementById("txtHint").innerHTML="";
  		return;
  	}
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null) {
	alert ("Your browser does not support XMLHTTP!");
  	return;
  	}
var url=url+"?admin="+str;
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;
}

 

gethint.php

<?php
echo $_SERVER['REQUEST_URI'];

$mysql = mysql_connect("localhost", "root", null);
mysql_select_db("openhouse", $mysql);

$query = mysql_query("SELECT name,admin FROM attendance WHERE training='no' ORDER BY name")or die("MySQL Error: ".mysql_error());

while($r = mysql_fetch_assoc($query)) {
$a[] = $r['admin'];
$b[] = $r['name'];
}
//get the q parameter from URL
$q=$_GET["q"];

//lookup all hints from array if length of q>0
echo '<table border="1">';
if (strlen($q) > 0)
  {
  $hint="";
  for($i=0; $i<count($a); $i++)
    {
    if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
      {
      if ($hint=="")
        {
        //$hint=$b[$i];
	echo '<tr>';
	echo '<td>'.$b[$i].'</td>';
	echo '<td><input name="chk" type="checkbox" value="'.$a[$i].'" onClick="chkit(this.value)" /></td>';
	echo '</tr>';
        }
      else
        {
        $hint=$hint." <br> ".$b[$i];
        }
      }
    }
  }
echo '</table>';

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
  {
  $response="no suggestion";
  }
else
  {
  //$response=$hint;
  $response = null;
  }

//output the response
echo $response;
?>

Link to comment
Share on other sites

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.