jwk811 Posted January 28, 2007 Share Posted January 28, 2007 im tryin to send data to the db using ajax but it wont let me...this is my form:[code]<html><head><script language="JavaScript" type="text/javascript">xmlHttp=GetXmlHttpObject();sendReq = getXmlHttpRequestObject();function addContent(){xmlHttp.open("GET", "process.php", true);sendReq.send('message=' + document.getElementById('content').value);}</script></head><body><form><input type="text" id="content" name="content" style="width: 447px;" /><input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:addContent();" /></form></body></html>[/code]and here is the process page:[code]<?php$content = $_POST['content'];$con = mysql_connect('mysql275.secureserver.net', '(my username)', '(my password)');if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("ajax_demo", $con); $sql = "INSERT INTO test(content, date) VALUES ("$content", NOW())"; db_query($sql);?>[/code]its not going in, could you tell me what the problem is ive been stuck on it for hours.. the problem could be in either the index or process page.. thank you very much for any help you could give me at all!! Quote Link to comment Share on other sites More sharing options...
ScotDiddle Posted January 29, 2007 Share Posted January 29, 2007 jwk811,I use FF and Firebug... Firebug said that GetXmlHttpObject(); and GettXmlHttpRequestObject(); were undefined.I played around with your code using an AJAX tutorial I found here: http://www.nmcmahon.co.uk/ajax/tutorial.php // (Page only displays in IE)(I changed the code ( posted below) to accept passed variables, instead of relying on hard-coded values found in the tutorial.When I ran the following, I got back: Quote Link to comment Share on other sites More sharing options...
ScotDiddle Posted January 29, 2007 Share Posted January 29, 2007 Sorry Folks, Must have Posted, when I meant to Preview, and I can't figure out how to edit on this board. ( Any help will be appreciated ;) ) Here is what I got back: "Database Insert Failed" ( because I did not create the table. )"$content = ( whatever user enters )"Try it out and let us know.Scot L. Diddle, Richmond VACalling HTML:[code]<html><head><script language="JavaScript" type="text/javascript">//// From: http://www.nmcmahon.co.uk/ajax/tutorial.php (Page only displays in IE)//function createRequestObject() { var req; if(window.XMLHttpRequest){ // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if(window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // There is an error creating the object, // just as an old browser is being used. alert('Problem creating the XMLHttpRequest object'); } return req;}// Make the XMLHttpRequest objectvar http = createRequestObject()// To assign this var (http) to an HTML object in your page, such as a button, input field,// or simply a link, then below is an example of how this can be done with our// sendRequest*() functions://// onClick="sendRequestViaPost('test1','script.php','$_POSTParm','action');" or// onChange="sendRequestViaGet('test2''script.php','$_GET Parm','action');"function sendRequestViaGet(divToWriteTo,scriptName,getParmName,action) { currentDiv = divToWriteTo currentScript = scriptName currntParmName = getParmName getRequest = action // Open PHP script for requests http.open('get', currentScript+'?'+currentParmName+'='+getRequest); http.onreadystatechange = handleResponseGet; http.send(null);}function sendRequestViaPost(divToWriteTo,scriptName,postParmName,action) { currentDiv = divToWriteTo currentScript = scriptName currentParmName = postParmName postRequest = action // Open PHP script for requests http.open('post', scriptName); http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); http.onreadystatechange = handleResponsePost; http.send(currentParmName+'='+postRequest);}// In the first example, when you click on a button, then it will send the request with a// value of 'test1' to our PHP script through GET, as in myphpscript.php?act=test1,// so you can then echo something relevant FROM that response.//// The second example will do exactly the same, except you choose an option from a drop down menu,// and send a different response, this time 'test2'.function handleResponseGet() { // The if() function below checks whether or not the readyState // of the XMLHttpRequest object is 4. This has many states: // // 0 being unintialised // 1 being loading // 2 being loaded // 3 being interactive // 4 being finished: the state we want. // // The http.status is the usual status returned by the server. // 200 is the one we are used to, when the page is OK. // You may know others such as 404, which is page cannot be found // or 500, internal server error. // When everything is OK, we make a variable called response that // stores the text returned by the PHP script. If this variable is true, // then we find the element in the document with the ID of ajaxTest and // replace the value inside with the value of response. if(http.readyState == 4 && http.status == 200){ // Text returned FROM the PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById(currentDiv).innerHTML = response; } }}function handleResponsePost() { if(http.readyState == 4 && http.status == 200){ // Text returned from PHP script var response = http.responseText; if(response) { // Update ajaxTest2 content document.getElementById(currentDiv).innerHTML = response; } }}</script></head><body><div id='requestDiv'> <form> <input type="text" id="content" name="content" style="width: 447px;" /> <input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="sendRequestViaPost('replyDiv','content.php','content', document.getElementById('content').value);" /> </form></div><!-- AJAX Reply will appear below --><div id='replyDiv'></div></body></html>[/code]content.php [code]<?php$content = $_POST['content'];// echo print_r($_POST);$insertQuery = new mysqli();$con = mysql_connect('localhost', 'root', '');if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("MySQL", $con); $sql = "INSERT INTO test(content, date) VALUES (\"$content\", NOW())"; if ($result = @$insertQuery->query($sql)) { echo "Content placed into Database"; } else { echo "<br \> \n Database Insert Failed<br \> \n"; echo "<br \> \n \$content = " . $content . "<br \> \n"; }?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.