php - How to store the the value of a text box in a database? -


what want achieve: in html window click button, popup window opens containing text box , submit button. there enter text text box, , after click submit button text should stored using sql in database.

i have code (see below)to text box value, , call script store value in database.

my ajax code

$(document).ready(function() { $("#sub").click(function() {     $.ajax({         type: "post",         url: "jqueryphp.php",         data: {             val: $("#val").val()         },         success: function(result) {             $("div").html(result);         }     }); }); });​ 

html form code

<input type="text" name="val[text]" id="val"/><br> <input type="button" name="sub"  value="submit" id="sub"/> 

how can put these pieces together?

you can use html form this:

<html>  <head>   <script type="text/javascript"     src="http://code.jquery.com/jquery-1.10.2.js"> </script>   <script type="text/javascript"     src="addentryformajax.js"> </script> </head>  <body>   <form id="form">     <input type="text" id="blogtext" name="blogtext" size="40" />     <input type="submit" id="submit" value="submit"/>   </form>    <div id="result">     none   </div> </body> 

the html form uses javascript attach submit handler (addentryformajax.js):

$(document).ready(function() {     $("#form").submit(function() {       doajax();        // prevent normal form submit       return false;     }); });  function doajax() {     $("#result").html("<span>calling ...</span>");      $.ajax({         type: "post",          url: "addentryajaxpdo.php",          datatype: "html",          data: {           blogtext: $("#blogtext").val()         },          success: function(result) {           $("#result").html(result);         }     }); } 

if submit button pressed, submit handler uses ajax call php script addentryajaxpdo.php inserts data database:

<div>  <?php  // sleep 3s, simulate long running request sleep(3);  $host = "localhost"; $db = "sandbox"; $user = "dev"; $pass = "dev";  $conn = new pdo("mysql:host=$host;dbname=$db", $user, $pass);  $stmt =   $conn->prepare(     "insert blog (blogtext, blogtimestamp) values (:blogtext, now())"); $stmt->bindparam(':blogtext', $_request['blogtext']); $stmt->execute();  $sql = "select last_insert_id()"; $query = $conn->query($sql); $row = $query->fetch(pdo::fetch_assoc); echo "inserted <em>" . $_request['blogtext'] . "</em> blog, id "    . $row['last_insert_id()'];  ?>  </div> 

the state/result of ajax call shown in html page in div element.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -