AJAX querystring in wordpress
-
I’m using the following javascript within my wordpress plugin, however the querystring doesn’t appear to work correctly.
Any ideas?
javascript:
‘<script type=”text/javascript”>
$(document).ready(function()
{
$(“#txtPostcode”).keyup(function()
{
var box=$(this).val();if(box.length >= 5)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(“txtPostcode”).value=xmlhttp.responseText;
}
}
xmlhttp.open(“GET”,”postcodechecker.php?q=”+box,true);
xmlhttp.send();}
return false;
});});
</script>’php:
‘<?php
/* Plugin Name: Postcode Checker
Plugin URI: [Enter your website URL]
Description: Validate postcode against database
Version: 1.0
Author: Gemma McCombe
Author URI: [Enter your website URL]
*/class PostcodeChecker extends WP_Widget {
function PostcodeChecker() {
$widget_ops = array(
‘classname’ => ‘PostcodeChecker’,
‘description’ => ‘Checks if valid postcode in database before request button enable’
);$this->WP_Widget(
‘PostcodeChecker’,
‘Postcode Checker’,
$widget_ops
);
}function widget($args, $instance) { // widget sidebar output
extract($args, EXTR_SKIP);
echo $before_widget; // pre-widget code from theme
print (‘<h2 class=”blocktitle”>Postcode Checker</h2> ‘);
print (‘<input type=”text” id=”txtPostcode” /> <input id=”btnRegister” type=”button” disabled=”disabled” value=”request application form” />’);
$q=$_GET[‘q’];
echo ‘
q=’ .$q. ‘
‘;$mysqli= mysqli_connect(‘localhost’,’root’, ‘My5ql5s5s’);
if (mysqli_connect_errno()) {
printf(“Connect failed: %s\n”, mysqli_connect_error());
exit();
}// A QUICK QUERY ON A FAKE USER TABLE
$query = “SELECT * FROM db_wp_webteam1.wp_bduk_towns where town_postcode=’BT11ER'”;
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);// GOING THROUGH THE DATA
if($result->num_rows > 0) {
//while($row = $result->fetch_assoc()) {
// echo stripslashes($row[‘town_postcode’]);
//}
echo ‘Results found’;
}
else {
echo ‘NO RESULTS’;
}// CLOSE CONNECTION
mysqli_close($mysqli);echo $after_widget; // post-widget code from theme
}}
add_action( ‘widgets_init’, create_function(”,’return register_widget(“PostcodeChecker”);’)
);
?>’
- The topic ‘AJAX querystring in wordpress’ is closed to new replies.