javascript, ajax, PHP insert onSubmit not working...POST not set -


i'm trying insert mysql database onsubmit() function... main part, function works. however, keeps inserting 1 db logbook_id field (the logbook_id should not 1 every time). here's (relevant) html:

echo '<div class="logbook_box" id="logbook'.$id.'"        style="display: block; overflow: hidden;">'; echo '<form action="cgi-bin/read_log.php" method="post"       onsubmit="return ajaxsubmit(this, ' . $id . ');">' .   '<input type="hidden" value="'.$id.'" id="id" name="id" />'. '<input style="float: right; width: 20%" type="submit" value="mark read" />' . '</form>'; 

here's onsubmit function:

<script type="text/javascript" src="cgi-bin/jquery-2.0.3.js"></script> <script> var ajaxsubmit = function(form, id) {     div = document.getelementbyid("logbook" + id);     div.style.display = "none";         // fetch want submit form         var url = $(form).attr('action');          var id = $('#id').val();      // setup ajax request     $.ajax({     url: url,     data: { id : id },     datatype: "json",     success: function() {         if(rsp.success) {             alert('marked read, thanks!');             }     }     });       // return false form not      // submit page      return false; } </script> 

and here's php file:

<?php  include('/var/www/olin2/includes/functions.php'); $con = connect_db(); session_start();  $id = isset($_post['id']); $username = $_session['username'];  $query = 'insert read_logbook(logbook_id, username)            values("'.$id.'",     "'.$username.'")'; mysqli_query($con, $query) or die(mysqli_error($con));   ?> 

so, <div> disappears should when submit clicked, shows id getting passed correctly. php script called, either doesn't input anything database, or (even more strangely) submit username , value 1 database if change php $_post $_get without changing method in form.

i'm new @ ajax/javascript, i'm sure has function, i'm confused , @ complete loss... ideas?

add type option post ajax since retriving value post...default get..

 $.ajax({   url: url,   data: { id : id },   type: "post", //<---here   .... 

and make sure id of logbook_box different , not 1 time

and change

$id = isset($_post['id']);   //this return either true or false. $id either 0 or 1 

try this

$id = isset($_post['id'])?$_post['id']:""; //or $_post['id'];  

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -