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');}?>
check_db_script.js
//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>