我正在尝试使用 AJAX 在 wordpress 中传递查询字符串,我已经使用 get 和 post 在网上尝试了各种代码。但是我无法在我的 PHP 中获取参数。
在我的页面中使用 javascript 是可行的,但是它会发回我想要避免的页面。有什么建议么?
回发的工作代码:
self.location = "?q=" + postcode;
或者
jQuery.param.querystring(location.href= "?q=" + postcode);
在php中使用$_POST['q']的ajax查询没有得到参数
jQuery.ajax({
type: 'POST',
url: 'wp-admin/admin-ajax.php' ,
data: {"q": postcode},
success: function(data){jQuery('#results').val(postcode);}
});
谢谢
芽
仍然有这个问题,无法将我的查询字符串放入我的 php:
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" />');
print ('<br/><input type="text" id="results"></div><br/>');
$postcode = $_GET['q'];
print $postcode;
$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
}
}
function add_js_to_wp_footer(){ ?>
<script type="text/javascript">
jQuery('#txtPostcode').keyup(function(){
var postcode = jQuery(this).val();
postcode = postcode.replace(/\s+/g, '');
if (postcode.length >= 5)
{
//self.location = "?q=" + postcode;
jQuery('#txtPostcode').keyup(function(){
var postcode = jQuery(this).val();
postcode = postcode.replace(/\s+/g, '');
if (postcode.length >= 5)
{
jQuery.ajax({
type: 'POST',
url: 'wp-admin/admin-ajax.php' ,
data: {"q": postcode},
success: function(data){jQuery('#results').val(data);}
});
}
return false;
});
}
return false;
});
</script>