php - Insert HTML input using ajax. Something wrong with code -
if click on submit button using php, data recorded in mysql.
through ajax _autosave.php update works. insert not work. going crazy.... can not understand
ajax code in first.php
<script type="text/javascript"> $(document).ready(function() { setinterval(function (){ var date_day1=$("#date_day1").val(); var amount1=$("#amount1").val(); data = 'date_day1=' + date_day1 + '&amount1=' + amount1; $.ajax({ type: "post", url: "_autosave.php", data: data, cache: false, /*success: function(){ $(".done").show().html("saved draft!"); }*/ }); settimeout(function(){ $(".done").hide(); }, 1000);// 15 seconds }, 3000);// 1 minute }); </script>
html input
<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>
part of php code identical in first.php , _autosave.php
$date_day1 = $_post['date_day1']; $amount1 = $_post['amount1']; if ($stmt = mysqli_prepare($mysqli, "select recordday 2_1_journal recordday = ? ")) { $stmt->bind_param('s', $date_day1); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($recordday); $stmt->fetch(); //echo $recordday .' $recordday<br>'; } if ($recordday == $date_day1) { 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<br>'; echo $amount1 .' amount1<br>';*/ } } else { if ($stmt = mysqli_prepare($mysqli, "insert 2_1_journal (recordday, amount, debitaccount, creditaccount) values(?,?,?,? )")) { $stmt->bind_param('sdss', $date_day1, $amount1, $debit1, $credit1 ); $stmt->execute(); //execute above insertion } }
update works in both files (called both files). insert works if called without ajax. wrong?
update found wrong. if $_post not set (not send), nothing recorded in mysql. no error message after execution. need remember variables here $stmt->bind_param('sdss', $date_day1, $amount1, $debit1, $credit1 ); must exist.
the data
syntax might reason, use format:
data: { key1: "value1", key2: "value2" }
see example from: http://api.jquery.com/jquery.ajax/
$.ajax({ type: "post", url: "some.php", data: { name: "john", location: "boston" } })
so, case try:
$.ajax({ type: "post", url: "_autosave.php", data: {date_day1:$("#date_day1").val(), amount1: $("#amount1").val()}, cache: false, });
Comments
Post a Comment