Jump to content

Ajax not running php in wordpress plugin


Recommended Posts

I'm not sure if this is a wordpress issue (and if it is I'll ask in that forum) or a javascript issue. I'm trying to use ajax to connect to a mysql database. The idea right now is to return a status of 'error' or 'success' after trying to connect to the database. It seems that when I try to login to the database what gets returned in $result['type'] is undefined. I would expect something other than that and I believe this is telling me that the php script was not ran. Anything I'm missing to get the php to run? Why is it not entering the php script? citools.php

<?php/*Plugin Name: CI Tools*///run action for both logged in and non-logged in usersadd_action("wp_ajax_check_db", "check_db");add_action("wp_ajax_nopriv_check_db", "check_db"); //check if the selected database has the proper tablesfunction check_db() {  $host = $_POST['host'];  $usr = $_POST['usr'];  $pwd = $_POST['pwd'];   //connect to mysql database using form values  $con = mysql_connect($host, $usr, $pwd);  if (!$con) {   $result['type'] = 'error';   echo $result;   die('Could not connect: ' . mysql_error());  }  $result['type'] = 'success';  $result['state'] = 'OK';   echo $result;   die();} //action to run after wordpress finishes loading but before headers are sentadd_action("init", "check_db_enqueuer"); function check_db_enqueuer() {  //register the js script to wp for use  wp_register_script('check_db_script', plugins_url('/js/check_db_script.js', __FILE__), array('jquery'));  //localize the script  wp_localize_script('check_db_script', 'MyAjax', array('ajaxurl' => admin_url('admin-ajax.php')));  //init script and dependancies  wp_enqueue_script('jquery');  wp_enqueue_script('check_db_script');}?>


//run only when document is readyjQuery(document).ready(function() { //when form is submitted run funcjQuery('#loginput').submit(check_db_ajax); //check db for needed tables, also verifies that you can loginfunction check_db_ajax() {  //check to make sure forms are filled in  if (jQuery('#sqlhost').val() == "" || jQuery('#sqlusr').val() == "" || jQuery('#sqlpwd').val() == "") {   jQuery('#dbstatus').html("Missing Fields!");   return false;  }  else {   //take form data and create a string for the php   var loginput = jQuery(this).serialize();   //make a jQuery ajax call   jQuery.ajax({	type: "POST",	url: MyAjax.ajaxurl,	data: loginput,	success: function(response) {	 if (response.type == "success") {	  jQuery('#dbstatus').html(response.state);	 }	 else {	  jQuery('#dbstatus').html(loginput+"\n"+response.type+"\nFAILED");	 }	}   });   return false;  }}})

html form

<form type="post" action="" id="loginput">	 Host: <input type="text" id="sqlhost" name="host"><br>	 Username: <input type="text" id="sqlusr" name="usr"><br>	 Password: <input type="password" id="sqlpwd" name="pwd"><br>	 <input type="hidden" name="action" value="check_db">	 <input type="submit" value="Login" id="loginbutton">	</form>	<div id="dbstatus">Database Status</div>

Link to comment
Share on other sites

If $result is an array, then if you try to echo it it will just print the word "Array". You should use your browser's developer tools to look at the ajax request so that you can see the data that gets sent to PHP, and the response from the server. I'm not sure what jQuery uses for the response object, if that is the XHR response object then everything that the server responds with will be in the response.responseText property. So with that code, I would expect that response.responseText would be the word "Array". If you want to return an entire array from PHP then you should use json_encode to serialize the array in JSON format, and use jQuery to convert response.responseText back into an array that you can check the values of. http://www.php.net/manual/en/function.json-encode.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...