0

i'm trying to post a form containing an input text, a textarea, and 14 checkbox with ajax. The idea is to insert some data according to the number of checked checkbox into table dbdata.

Here is the script.js :

$(document).on('click', '#save', function(event) {
//action
var url     = "aksi.php";
//an input text
var v_id    = $('input:text[name=id]').val();
//a textarea
var v_note  = $('#note').val();
//some checkboxes
var v_checkbox =[]
    $("input[name='checkbox[]']:checked").each(function ()
    {
        v_checkbox.push(parseInt($(this).val()));
    });

//ajax
$.ajax({
        url: url,
        type: "POST",
        data: {id: v_id,note: v_note,checkbox:v_checkbox },
        success: function (data, status){
            alert("success!")
        },
        error: function(xhr,err){
            alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
            alert("responseText: "+xhr.responseText);
        }
    });

});

This is the aksi.php :

require "../../config/db.php";
$id=$_POST['id'];
$checkbox= $_POST['checkbox'];
$note=$_POST['note'];
foreach ($checkbox as $key => $check_box) {
mysqli_query($con,"INSERT INTO dbdata(id, checkbox, note) VALUES($id, $check_box, '$note')");
}

The problem is the data never posted. The ajax thrown error (readyState=0, status=0) and data not inserted. The url also changed to :

index.php?checkbox[]=3&checkbox[]=4&checkbox[]=5

depend on which checkbox are checked. Any idea?

userone912
  • 13
  • 2
  • convert v_checkbox to json – madalinivascu Apr 09 '15 at 07:52
  • There's no need to use JSON for this. – Quentin Apr 09 '15 at 08:12
  • The URL looks fine. `readyState=0` suggests the browser is cancelling the request because you're violating the same origin policy or quitting the page as soon as you trigger the Ajax request. What does the browser's Javascript console say? – Quentin Apr 09 '15 at 08:13
  • hmm, it won't be because you are quitting the page: you wouldn't see the error function triggering then. – Quentin Apr 09 '15 at 08:17
  • **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Apr 09 '15 at 08:18
  • @Quentin nothing in console, the page directed to index.php?checkbox[]=1...etc after i close the alert – userone912 Apr 09 '15 at 08:21
  • can you post HTML mark up? – mehany Apr 09 '15 at 08:21
  • That sounds like the form is being submitted and is redirecting you, but that shouldn't happen *after* the alert because the request hasn't be made synchronous so it shouldn't block. – Quentin Apr 09 '15 at 08:22
  • Although `readyState=0` means it probably never actually fires the request in the first place, which would allow the error to run before the form submission. Check the configuration of your error console. Is it set to clear the console on page load? I suspect there is an error message there but it does away before you look at it. – Quentin Apr 09 '15 at 08:24
  • @Quentin how do i make sure if the $_POST succesfully passed to aksi.php? – userone912 Apr 09 '15 at 08:34
  • You look at the Net tab of your browser's developer tools. – Quentin Apr 09 '15 at 08:55

1 Answers1

0

Can you put one echo statement at the last in php file for resolving the error issue. like

echo 1;

Update the first line like this if save is the submit button:

$(document).on('click', '#save', function(event) {
 event.preventDefault();
 .....

});

For resolving the db insertion issue, try include the full path of the required file instead of relative path.

Sanjay Kumar N S
  • 4,653
  • 4
  • 23
  • 38