Reputation:
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
Upvotes: 1
Views: 424
Reputation: 20267
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)
Upvotes: 4
Reputation: 3167
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.
Upvotes: 0
Reputation: 152
$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'];
Upvotes: 0
Reputation: 193261
In javascript variables are not interpolated inside the strings like in PHP. So this
{ "CONTROL": "len" }
should be
{ "CONTROL": len }
Upvotes: 0