tags:

views:

37

answers:

2

I need help with posting a value to a php script from a js function called by an onclick event in an HTML page. Any help would be appreciated.

I have a link to the Google hosted jQuery library.

I am rendering a page with php with a submit button whose onclick action is seen below as updateSession() which posts a value to a php script that is supposed to update a MySQL table.

I can step through the script wile executing, using Fire Bug, but once it steps back out of the jQuery library no action or messages that I can see.

---------- script details

Part of the echo statement from the php rendered html page:

<input type=\"submit\" value=\"Submit\" class=\"close\" onClick=\"checkCode()\"/>

The js function called by the submit action (i commented out my original attempt but left in place for comments):

function updateSession (){
 //$.post( "includes/update.php", { action: "y" });
 jQuery.ajax({
  url: "includes/update.php",
  type: "POST",
  processData: false,
  contentType: "text",
  data: { 'action': 'y'},
  success: function( data ) { // for debugging
   alert( data );
  }
 });
}

The php script being called with the jQuery .ajax:

<?php
 //session_start();
 require("includes/config.php");
 $db = mysql_connect($dbhost, $dbuser, $dbpassword);
 mysql_select_db($dbdatabase, $db) or die ( "Data Server Error");
 // connect to db
 // get session id from $_POST parm
 // add session id + validation marker to sessiondata table
 $action = mysql_real_escape_string($_POST['action']);
 //$idSession = SESSION_ID();

 $idSession = "sdfwe54645gaerg";
 $setCouponStateSql = "INSERT INTO appdata values ($idSession, $action);";
 $executeUpdate = mysql_query($setCouponState);

?>
A: 

i am not what exactly is the problem .. but the php script doesnt send any output so in js u wont be seeing anything in that alert(data); it should come back as an empty alert.

put echo "something"; in the php script at the end and then test.

Sabeen Malik
A: 

Try using firebug (use the 'net' tab) to see what URL is being posted by the AJAX call.

  • If the URL doesn't look right, then you have an issue with your ajax call.

  • If the URL looks correct, paste it into your browser and observe the behavior of your PHP code.

You should be able to debug from there.

BryanH
Thanks for the firebug tip. Knowing the tools better helps. I really appreciate the tip.
jeff