Why with ajax I can update mysql but cannot use value from mysql/php variable? -
all happens in 1 php file
html code
<td> <input type="text" name="date_day1" id="date_day1" value="<?php echo $_post['date_day1']?>" size="1"> </td> <td> <input type="text" name="amount1" id="amount1" value="<?php echo $_post['amount1']?>" size="5"></td>
then javascript
<script type="text/javascript"> //cross-browser xmlhttp getter function gethttpobject() { var xmlhttp; // variable makes ajax possible! //global xmlhttp request object //creating object of xmlhttp in internet explorer try { xmlhttp = new activexobject("msxml2.xmlhttp"); } catch(e) { try { xmlhttp = new activexobject("microsoft.xmlhttp"); } catch(oc) { xmlhttp = null; } } if (!xmlhttp && typeof xmlhttprequest != 'undefined') { try { // set var new request opera 8.0+, firefox, safari xmlhttp = new xmlhttprequest(); }//try { catch (e) {//if fails move onto next xmlhttp = false; }//catch (e) { }//if (!xmlhttp && typeof xmlhttprequest != 'undefined') { return xmlhttp; }//function gethttpobject() { function init(){ window.setinterval(autosave,3000); // 15000=15 seconds } function autosave(){ var date_day1 = document.getelementbyid("date_day1").value; var amount1 = document.getelementbyid("amount1").value; var params = "date_day1="+date_day1+"&amount1="+amount1; var http = gethttpobject(); http.open("post", window.location.href, true); http.setrequestheader("content-type", "application/x-www-form-urlencoded"); http.setrequestheader("content-length", params.length); http.setrequestheader("connection", "close"); http.send(params); } </script>
and php
$date_day1 = $_post['date_day1']; $amount1 = $_post['amount1']; $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname); if($_server["request_method"]=="post"){ if ($stmt = mysqli_prepare($mysqli, "update 2_1_journal set amount = ? recordday = ? ") ) { $stmt->bind_param( 'ds', $amount1 , $date_day1 ); $stmt->execute(); echo $date_day1 .' date_day1 update<br>'; echo $amount1 .' amount1<br>'; } }
so happens. javascript (ajax) without clicking button takes user input, send php code. php code updates mysql. means somehow without clicking submit button created php variables update ($amount1 , $date_day1)?
but why these variables not exist latter? want without page refresh (without clicking submit button) use variables. example in input form value=""
how that? understand need use json? information have found can not understand.... can someon write step step how json pass user input (or value mysql) input value=""
or may book/tutorial how works (book/tutorial dummies understand)?
your php script should echo json. example, have ajax.php:
<?php $myvariable = $_post['myvar']; //do database here //here goes whatever want pass page echo json_encode(array('result' => 'done doing things', 'data' => $somedata)). // outputs json // {"result" : "done doing things", "data" : "contents of $somedata"}
and jquery process this. html file like
<html><head> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> $(document).ready(function() { $('.mybutton').click(function(e) { e.preventdefault(); //prevent page reloading $.post('ajax.php', function(data) { $('#mycontent').html(data.result); //put result div }, 'json'); }); }); </script></head> <body> <div id="mycontent">this text replaced</div> <a href="#" class="mybutton">click me</a> </body> </html>
read here jquery post function. http://api.jquery.com/jquery.post/
don't forget include jquery library (read documentation thoroughly).
if need data not erased between calls, can :
- pass in json , store in javascript variable,
- store in session or cookie
- store in database
Comments
Post a Comment