它不一致,但对于某些用户,它会不时失败,并且用户也会在远程桌面上失败。
我认为这是正确的,但并不完全确定。 PLease建议我做什么,以及是否有错误。
这是一个简单的插件,可以将输入与数据库表进行比较,并在不刷新页面的情况下返回相应的结果。
来自.php
add_action( 'wp_enqueue_scripts', 'inputtitle_Submit_scripts' );
add_action( 'wp_ajax_ajax-inputtitleSubmit', 'eidlookup_inputtitleSubmit_func' );
add_action( 'wp_ajax_nopriv_ajax-inputtitleSubmit', 'eidlookup_inputtitleSubmit_func' );
add_shortcode("eidlookup", "eid_lookup_form");
function inputtitle_Submit_scripts() {
// wp_enqueue_script( 'input_submit', eidlookup_plugin_url . '/js/input_submit.js', array( 'jquery' ));
wp_enqueue_script( 'input_submit', plugin_dir_url( __FILE__ ) . '/js/input_submit.js', array( 'jquery' ));
wp_localize_script( 'input_submit', 'eid_Ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nextNonce' => wp_create_nonce( 'eidajax-next-nonce' ))
);
}
/* Lookup in database */
function eidlookup_inputtitleSubmit_func() {
// check nonce
$nonce = $_POST['nextNonce'];
if ( ! wp_verify_nonce( $nonce, 'eidajax-next-nonce' ) )
die ( 'Beklager, det lykkedes ikke. Genindlæs siden (Ctrl-F5 på pc) og prøv igen.');
$zipcode = $_POST['zip'];
// generate the response
global $wpdb;
$tablename = "{$wpdb->prefix}levering";
$sql = "SELECT Levering FROM {$tablename} WHERE Zip LIKE %s";
$result = $wpdb->get_col($wpdb->prepare($sql, $zipcode));
//error handling
if ($result != Array()) {
$response = array();
foreach($result as $res){
$response = $res;
}
// response output
echo "Vi leverer i postnummer " . $zipcode . ":<br /><strong>" . $response . "</strong>";
}else{
echo "Det indtastede postnummer blev ikke genkendt - kontroller venligst og prøv igen.";
}
die();
// IMPORTANT: don't forget to "exit"
exit;
}
/* define formular, add shortcode
* [eid-lookup] - no args
*/
function eid_lookup_form(){
ob_start();
?>
<div class="form-signing">
<label>Postnummer:</label><br />
<input type="text" name="eid-lookup-input" id="eid-lookup-input" width="100px" /><br />
<button class="btn btn-large" id="button">Vis dage</button>
</div>
<div id="info"></div>
<?php
return ob_get_clean();
} //end eid_lookup_output
来自input_submit.js:
jQuery(document).ready(function($){
$('#button').click(function(){
$.post(
eid_Ajax.ajaxurl,
{
// wp ajax action
action : 'ajax-inputtitleSubmit',
// vars
zip : $('input[name=eid-lookup-input]').val(),
// send the nonce along with the request
nextNonce : eid_Ajax.nextNonce
},
function( response ) {
console.log( response);
$("#info").html(response);
}
);
return false;
});
});