Jump to content

jcj

New Members
  • Posts

    2
  • Joined

  • Last visited

    Never

Everything posted by jcj

  1. It's basically the same as my previous example.. In this version I created a table named "demotbl" in the database "demo" with a demo_pk (auto increment) field and a descr (text) field. form page: <html> <head> <script type="text/javascript"> function getMessageResponse(str) { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById('response').innerHTML=xmlHttp.responseText; document.myform.message.value = ''; } } var url="new.php"; url=url+"?message="+str; url=url+"&sid="+Math.random(); xmlHttp.open("GET",url,true); xmlHttp.send(null); } </script> </head> <body> <form method="POST" name="myform" id="myform" onSubmit="return false;"> <input type="text" name="message"> <input value="Post!" type="submit" onclick="getMessageResponse(document.myform.message.value);"> </form> <div id="response" name="response">Response will be placed here on submit.</div> </body> </html> PHP page (note that you would want to include error checking in this for your final version.. This is just a quick demo.) <?php if(isset($_GET['message'])) { /* this code is missing all error/sanity checks. Just a quick and dirty * example of inserting a record and echoing the complete table back. */ //Connect $con = mysqli_connect("localhost", "demo", "demodemo", "demo"); //Set the message variable that we will insert into the database. $message = mysqli_real_escape_string($con, $_GET['message']); //Set up and execute the insert query. $insertquery = "insert into demotbl(descr) values('$message')"; mysqli_query($con, $insertquery); //Set up and execute the select query to return all the values including //the new one. $selectquery = "select * from demotbl order by demo_pk"; $result = mysqli_query($con, $selectquery); //echo the results from the select query while ($row = mysqli_fetch_assoc($result)) { echo $row["demo_pk"] . " " . $row["descr"] . "<br />"; } //Free result and close connection. mysqli_free_result($result); mysqli_close($con); } ?>
  2. I put together a quick AJAX sample based on your form. Here is your form with the AJAX function in the head. <html> <head> <script type="text/javascript"> function getMessageResponse(str) { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { document.getElementById('response').innerHTML=xmlHttp.responseText; document.myform.message.value = ''; } } var url="new.php"; url=url+"?message="+str; url=url+"&sid="+Math.random(); xmlHttp.open("GET",url,true); xmlHttp.send(null); } </script> </head> <body> <form method="POST" name="myform" id="myform"> <input type="text" name="message"> <input value="Post!" type="button" onclick="getMessageResponse(document.myform.message.value);"> </form> <div id="response" name="response">Response will be placed here on submit.</div> </body> </html> And the sample new.php I used for testing. <?php if(isset($_GET['message'])) { $received = $_GET['message']; echo "I received: $received."; } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.