Jump to content

deadpickle

Members
  • Posts

    3
  • Joined

  • Last visited

deadpickle's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. Your absolutely right about the json_encode, as soon as I converted it, everything worked. thank you much.
  2. 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>
  3. I'm pretty new to javascript/ajax/php/html so bear with me.I am Trying to use ajax to connect to a database. The reason I am using ajax is so I can dynamically update the webpage as information is gathered about the database. So far I have is setup so that when a button is clicked: <input type="button" value="Login" onclick="sqllogincheck(this.form)"> a function is ran that will connect up to the database server side: <html><head><title>High Plains Regional Climate Center</title><link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>"><?php wp_head(); ?></head><!--All functions for pages--><script>//login and get mysql tables, update table status viewfunction sqllogincheck(frm) {var xmlhttp; //All fields must be fullif (frm.sqlusr.value == "" || frm.sqlhost.value == "" || frm.sqlpwd.value == "") { alert("Missing Fields!");}//use AJAX to setup a query to the serverelse { xmlhttp=new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("dbstatus").innerHTML=xmlhttp.responseText; } } //send POST request to server xmlhttp=open("POST","citools/login.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("sqlusr=" + frm.sqlusr.value + "&sqlhost=" + frm.sqlhost.value + "&sqlpwd=" + frm.sqlpwd.value); //alert("Username: " + frm.sqlusr.value + "\nHost: " + frm.sqlhost.value + "\nPassword: " + frm.sqlpwd.value)}}</script><body><div id="wrapper"> <div id="header"> <img src="<?php bloginfo('template_directory'); ?>/images/headers/top_logo.jpg" alt="HPRCC"> <!--<div id="menu"> <?php wp_nav_menu( array( 'theme_location' => 'header-menu', 'container_class' => '') ); ?> </div>--> <?php shailan_dropdown_menu(); ?> <h1 id="pagetitle"><?php wp_title(); ?></h1> </div> When I test this code out I get the error "Uncaught TypeError: Object [object Window] has no method 'setRequestHeader'". I have the vague idea that this means there is no 'setRequestHeader' method in the xmlhttp object, but im not sure how to correct it.Just a bit more info here. I'm testing this page using localhost and am not sure if that has anything to do with the error. Also, I'm using Wordpress for my CMS and this is why I only posted the header here.Thanks for the help.
×
×
  • Create New...