This a question that synthesizes two questions that remain unlsoved :
I am trying to implement some ajax method to post some comments on a webpage. I use the ajax method like such :
<button id="my-btn">Make an Ajax request!</button>
<script >
$('#my-btn').click(function() {
var comment = $('#id1').val();
var m = {$id2};
var data = new Array();
data[0]= comment;
data[1]= m;
$.post('{$postURL}', data, function(callback_data){
alert('hello');
});
});
</script>
where m = {$id2}; is due to a smarty variable.
The alert('hello') works, but the php code is not processed : {$postURL} requires a method comment(){$comment = $_POST[$data[0]]; $m = $_POST[$data[1]];...}. So, postURL is like : "index.php?post=comment", and the method is comment(). Of course, when I replace {$postURL} by
"index.php?post=comment", nothing happens in the sense that I still have the alert('hello') message but the method comment() doesn't process anything. Is this method evenc called ? Or is there a wrong syntax such that $_POST[$data[0]] and $_POST[$data[1]] aren(t recognized bt the comment() method.
The way index.php works is to redirect : to a another php page, call it mypage.php where we can find the comment() method.
Moreover, something very weird : when I corrupt $.post("{$postURL}" by $.post("{$whatever}", I still have a alert('hello') message ! And weirder, when I put an alert(callback_data); inside the callback function, I get a huge alert message, which consists of my whole php code...
Best,
Newben
Smarty uses those curly braces so you need to pop them in using {ldelim} and {rdelim}, also you aren't sending post data as an assoc array so $_POST['comment'] etc won't be set - try this instead:
<script>
$('#my-btn').click(function() {ldelim}
var comment = $('#id1').val();
var m = {$id2};
var data = {ldelim}
comment: $('#id1').val(),
m: {$id2}
{rdelim};
$.post("{$postURL}", data, function(callback_data){ldelim}
alert('hello');
{rdelim});
{rdelim});
</script>
Related
I am having trouble with a bit of code that uses the ajax $.post function to send a name to a php file which then does some stuff with it. I believe the issue to be in the ajax code, because I have found that the posted value never makes it to the $_POST array (i.e. the $_POST array is not set). However, I do not think that there are any syntactical mistakes on either end, so I am confused as to why it does not work.
Here's the jQuery.
if ($(e.target).hasClass('shootNames')) {
var shootName = $(e.target).attr('id');
var par = $('#' + shootName).parent().attr("id");
$.post("displayImages.php", {shoot: shootName}); //the information I would like to send
$('#' + par).load("displayImages.php").off('click', $('#' + par));
}//end if hasClass
And the relevant bit of php.
if (isset($_POST['shoot'])) {
$shootname = $_POST['shoot'][0]; //pick out just the first member of the $_POST array
$filepath = "images/u/$foo/$shootname";
$f->FilesAsImages($filepath);
}//end if
Thanks for any help.
You're hitting displayImages.php once in your $.post call and there it should return your image, but you don't have a success handler in there, so it does nothing with the result. You're then hitting displayImages.php again with .load and not passing it anything, so it does nothing as expected. Adding a success handler to your $.post call and doing the work in there, will do what you want.
See the $.post documentation for more: http://api.jquery.com/jquery.post/
Sample code:
if ($(e.target).hasClass('shootNames')) {
var shootName = $(e.target).attr('id');
var par = $('#' + shootName).parent().attr("id");
$.post("displayImages.php", {
shoot: shootName
}, function(data, textStatus, jqXHR) {
// this is the success function and the 3 arguments it gives you, remove the 2nd/3rd if you don't use them
$('#' + par).html(data).off('click', $('#' + par));
});
}
Try change from this
$.post("displayImages.php", {shoot: shootName}); //the information I would like to send
to this
$.post("displayImages.php", {'shoot[]': shootName}); //the information I would like to send
Source :
http://api.jquery.com/jquery.post/
I am trying to pass the value of a parameter outside a $.get jQuery in javascript. I read that I should somehow do that in synchronous mode but I couldn't find a solution.
Here is my code:
var flag;
var t = $.get("mutalyzer.php?id="+variation.value, function(data) {
flag=data;
});
document.write(flag);
where I get undefined as a result.
Thanks!
write it inside the callback function
try this
var t = $.get("mutalyzer.php?id="+variation.value, function(data) {
//this is the callback function and runs when get gets completed
flag=data;
document.write(flag);
});
I'm new to jQuery and Ajax and I have run into a problem. Im getting the error on my console that:
NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument # http://localhost
/jquery.js:7065
Why am I receiving this error?
this is the code Im Using:
function upload_file(){
var file = document.form1.file_upload;
var date = document.form1.date_added;
var author = document.form1.author;
var user = document.form1.user;
var semester = document.form1.semester;
var class1 = document.form1.class;
var subject = document.form1.subject;
$.ajax({
type:"get",
url:"upload_file.php",
data:{
"file":file,
"date":date,
"author":author,
"user":user,
"semester":semester,
"class":class1,
"subject":subject
},
success:function(result){
$("#result").html(result);
}
});
}
Im waiting for your replies.
PS: I Did search the forum but did not get what i want, so if i missed something, sorry in advance.
I think the problem is you are trying to pass complete objects to the JSON.
You should use values instead of objects. For example, replace:
var subject = document.form1.subject;
with:
var subject = document.form1.subject.value;
Use this , i guess bracket mismatch --
$.ajax(
{
type:"get",
url:"upload_file.php",
data:{
"file":file,
"date":date,
"author":author,
"user":user,
"semester":semester,
"class":class1,
"subject":subject
},
success:function(result)
{
$("#result").html(result);
}
);
I ran into the same error, but my problem was different.
Turns out, I was passing a parameter in the ajax call that wasn't present in my DOM at all.
In #ZackValentine-s case (or for someone reading this in the future), please check the value of all the data items you are about to pass to the ajax call, BEFORE the actual call itself.
We had the same error.
Upgraded to the latest version of JQuery and problem solved.
This one seems to work for some people seen that solution here too
It's been some time I am looking for an answer but as far as today, nobody found :
I am trying to implement some ajax method to post some comments on a webpage. I use the ajax method like such :
<button id="my-btn">Make an Ajax request!</button>
<script >
$('#my-btn').click(function() {
var comment = $('#id1').val();
var m = {$id2};
var data = new Array();
data[0]= comment;
data[1]= m;
$.post('{$postURL}', data, function(callback_data){
alert('hello');
});
});
</script>
where m = {$id2}; is due to a smarty variable.
The alert('hello') works, but the php code is not processed : {$postURL} requires a method comment(){$comment = $_POST[$data[0]]; $m = $_POST[$data[1]];...}.
So, postURL is like : "index.php?post=comment", and the method is comment().
Of course, when I replace {$postURL} by "index.php?post=comment", nothing happens in the sense that I still have the alert('hello') message but the method comment() doesn't process anything.
Is this method evenc called ? Or is there a wrong syntax such that $_POST[$data[0]] and $_POST[$data[1]] aren(t recognized bt the comment() method.
The way index.php works is to redirect : to a another php page, call it mypage.php where we can find the comment() method.
Moreover, something very weird : when I corrupt $.post("{$postURL}" by $.post("{$whatever}", I still have a alert('hello') message ! And weirder, when I put an alert(callback_data); inside the callback function, I get a huge alert message, which consists of my whole php code...
In a precedent question, I was told to put some delimiters because of Smarty like so :
$('#my-btn').click(function() {ldelim}
var comment = $('#id1').val();
var m = {$id2};
var data = {ldelim}
comment: $('#id1').val(),
m: {$id2}
{rdelim};
$.post("{$postURL}", data, function(callback_data){ldelim}
alert('hello');
{rdelim});
{rdelim});
But, nothing changed,
Anybody has an idea ?
Best, Newben
Problem can be that you are passing data as javascript array, but it should be a map or string as mentioned here.
Try this :
<script >
$('#my-btn').click(function() {
var comment = $('#id1').val();
var m = {$id2};
$.post('<?php echo $postURL ?>', {0 : comment,1 : m}, function(callback_data){
alert('hello');
});
});
</script>
I'm a stuck with the following function:
<script type="text/javascript">
function removeElement($parentDiv, $childDiv){
if (document.getElementById($childDiv)) {
var child = document.getElementById($childDiv);
var parent = document.getElementById($parentDiv);
parent.removeChild($child);
}
}
</script>
x
This function deletes a child element, and its content, which works great client-side! But I am wanting to pass a value to the server, in the same instance, so the content of the element can be deleted from the mysql database too. I have no idea how to do this, so any suggestions will be very appreciated!
Notes: $child, and $parent are strings generated within the php file, that I use to give each element a unique ID.
To make your life easier, use jQuery or similar framework. Here's how you would do it in jQuery:
$(function() {
$('.delete').click(function() {
var link = $(this);
var id = link.attr('id').replace('element_', '');
$.ajax({
url: 'handler.php',
data: {
element: id
},
type: 'post',
success: function() {
link.remove();
// Or link.closest('tr').remove() if you want to remove a table row where this link is
}
});
return false;
});
});
The HTML:
Remove
And handler.php:
mysql_query("DELETE FROM `table` WHERE id = '".mysql_real_escape_string($_POST['element'])."'");
Always remember to escape database input!
If you're a total noob as you said, you probably won't understand all of this so I suggest you read something about jQuery's AJAX capabilities and about overall development using jQuery or similar JavaScript framework.
Lets say I want to delete an entity using a ID
JQUERY - $.post()
This is an easy way to send a simple POST request to a server without having to use the more complex $.ajax function. It allows a single callback function to be specified that will be executed when the request is complete (and only if the response has a successful response code). Jquery post docs
On the server assuming you have an open database connection.
mysql_query("DELETE FROM TABLE WHERE ID = ".$_POST['ID']);
more on mysql_query found here
EDIT:
So the following will only remove the element when the ajax post is complete. Note the first arg is the url to the script that will take the action , second is the data to be sent, in this case the ID post value will be {child.id} and the third is a anon inline callback function that will take action to remove the element client side.
<script type="text/javascript">
function removeElement($parentDiv, $childDiv){
if (document.getElementById($childDiv)) {
var child = document.getElementById($childDiv);
var parent = document.getElementById($parentDiv);
$.post('{URLTOSCRIPT}', 'ID=$child.id',function () { parent.removeChild($child); });
}}
</script>
When you call the function, you'd want to put your PHP variables in tags like so:
<?php echo $parent; ?>
and
<?php echo $child; ?>
In the function definition, you will want to get rid of the PHP style variables and use something like:
function removeElement(parentDiv, childDiv) {
//CODE
}