1

I am new to jquery also to php so my question sound quite simple or silly. Any help would be appreciated. I have an html in where i add and delete rows via buttons. And I want to update database by these data by another button. So i need the get length of my table to insert them within a loop in php side. How can i tranfer data to php side? I tried something like that.. As javascript code

function Length()
       {

            var table=document.getElementById("Table");
            var len = table.rows.length;
            $.post('/myproject/update_data.php', { "CONTROL": "len" } );
       }

on html;

<input type="submit" name="UPDATE" id="UPDATE" value="UPDATE" onclick="return Length();"/>

on php side it goes like

<?php
if(isset($_POST["UPDATE"]))
               {    
                    $length=$_REQUEST["CONTROL"];
               }
?>

And i get CONTROL is not defined. Is it a syntax wrong that i type directory of php file? Or any thing else? I couldnt get. Thank you

3
  • Try taking CONTROL out of quotes in your .post call. Commented Feb 15, 2013 at 14:49
  • 3
    Do you expect the form not to submit just because you return a function ? Commented Feb 15, 2013 at 14:50
  • Yeah, try using a button, and then if you still need to "submit" the form, do it in your JavaScript AFTER your other stuff gets done. Commented Feb 15, 2013 at 14:54

4 Answers 4

4

You can't mix an AJAX post with a form submission, those are two separate requests. The form is submitting before the AJAX request is completed, so the form submit is the request your server receives.

I'd recommend switching entirely to an AJAX request and add return false; to the end of the function, which will cancel the form submit and allow the AJAX request to do the work itself. Like this:

$.post('/myproject/update_data.php', { "CONTROL": len, "UPDATE": "UPDATE" } );

Alternatively, you can skip the AJAX and use JS to add a hidden input to your form.

Add this to your HTML:

<input type="hidden" name="CONTROL" val="">

Add this to your JS:

$('[name="control"]').val(len)
Sign up to request clarification or add additional context in comments.

3 Comments

So it changes dynamically in a function CONTROL's value. Can i just write down like that?
Added examples of how you would implement each option, does that help?
Thank You i changed the code as you saind above it worked. Thanks alot!
0

In javascript variables are not interpolated inside the strings like in PHP. So this

{ "CONTROL": "len" }

should be

{ "CONTROL": len }

Comments

0
$length=$_REQUEST["KONTROL"];

$.post('/myproject/update_data.php', { "CONTROL": "len" } );

"KONTROL" != "CONTROL"

instead of: $.post('/myproject/update_data.php', { "CONTROL": "len" } );

replace: $.post('/myproject/update_data.php', { "CONTROL": len } );

"len" is only string.

and instead of: $length=$_REQUEST["KONTROL"];

replace: $length = $_POST['CONTROL'];

Comments

0

Try to change this section of code here

function Length()
   {

        var table=document.getElementById("TableYetkili");
        var len = table.rows.length;
        $.post('/myproject/update_data.php', { "CONTROL": "len" } );
   }

Change that to this

function Length()
   {

        var table=document.getElementById("TableYetkili");
        var len = table.rows.length;
        $.post('/myproject/update_data.php', { "CONTROL": len } );
   }

I think the problem you were having is that the len is not a string, but the var len, no need for quote marks. Hope I helped. Also in your PHP file, if the form is not being submitted, the IF statement will not be true.

2 Comments

Thanks for help. I change the code as you said. Still get the undefined index error. Do you think anything else could be the problem? I have html and php files in different folders. Does it cause a problem in that way?
As long as you put the right paths it should be fine folder wise. My next guess is that your not submitting the form correctly, as Brad Koch said switch to either fully AJAX sending or use the default form submitting method.