Hello i have searched the whole website for a soltution found something but didnt get it to work.
-I have the main function on the head area of the page. (This will return Data from a PHP)
-This Data i need as a variable but dont know how to handle.
function note(content,usern){
note = Function("");
$.ajax({
type: "POST",
url: "note.php",
data: {'token': content, 'user': usern }, success: function(data){ var myneededvar = data; }, }); }
Ok thats works and also data is given out
Now im calling the function in the script like this
note(token,notename);
and i need the result myneededvar
but cant get it to work.
Firstly, your variable myneededvar is local to the success handler function and will not be available outside.
Secondly, your AJAX call is asynchronous and you cannot expect to immediately get the AJAX return data in a variable right after the AJAX call statement.
i.e., you cannot do:
note(...); // call your method
alert(myneededvar); // this won't work as the AJAX call wouldn't have completed
Thirdly, not sure why you have that note = Function(""); statement there. You should remove that.
Something like this should work:
var myneededvar;
function note(content, usern){
$.ajax({
type: "POST",
url: "note.php",
data: {'token': content, 'user': usern },
success: function(data){
myneededvar = data;
// use it here or call a method that uses myneededvar
}
});
}
Related
I am fairly new in the Ajax's world, tho I've tried it maybe twice and it always worked like a charm. Now I am trying to send variable with ajax() method but it seems like I have 0 errors in my console but I think the problem is that I am sending no variable at all.. If, in my php file, I echo a string it's working. So my problem is that I can't echo out the variable. I am on Laravel 5, this is why you will see Request::get('my_input_name').
Here is my js code :
$('.select_blocs_check').click(function() {
var blocID = $(this).val();
$.ajax({
type: "POST",
url: "http://localhost/send/getBlocHtml/",
data: {id: blocID},
success: function(html) {
$('#preview-wrap').html(html);
}
});
});
This is my php file
public function getBlocHtml()
{
$bloc_id = Request::get('id');
echo $bloc_id;
}
So, if I change my php file like this
public function getBlocHtml()
{
$bloc_id = Request::all();
print_r($bloc_id);
}
Now, it will print out : array(). Like if I have nothing in my data.. What's wrong with my data parameter in $.ajax ?
I see Laravel 5 in your question. Try Request::input('id'); Which will pull both Post and get.
Change how you pass your data. Try this :
$.ajax({
type: "POST",
url: "http://localhost/send/getBlocHtml/",
data: {'id': blocID},
success: function(html) {
$('#preview-wrap').html(html);
}
});
try change Request::get('value') to Input::get('value')
API says
This method is used for all request verbs (GET, POST, PUT, and DELETE)
In Laravel 5 API is Input static method.
And article which may help to you.
I am trying to understand a $.ajax call:
var url = "/api/followuser.php/" + userID ;
$.ajax(url, { 'success': function(data) {
/do something
}
});
Thia ajax call is required to pass the variable 'userID' to the file '/api/followuser.php' to make a database query(php/Mysql).
I don't have access to '/api/followuser.php' .
Can anyone help me figure out how to get the variable 'userID' from the URL in a php file to be used in a database query.( I know how to pass variable as 'data: userID,' in $.ajax and use it in a php file but i want to understand this particular example)
Maybe you mean followuser.php?user_id= instead? The slash is probably causing issues since that's interpreted as a directory by the server:
var url = "/api/followuser.php?user_id=" + userID;
you need to use GET method with ajax, to do this you can use next example
$.ajax({
url: "/api/followuser.php",
type: "GET",
data: {variable: "valueofvariable"},
success: function(data) {
console.log(data);
}
});
so in your php file you can read the variable like this
<?php
if(isset($_GET["variable"])){
echo $_GET["variable"];
// if this works you should see in console 'valueofvariable'
}
?>
I have a PHP program for counting user banner clicks. My banner link is something like this:
<a href="<?=$banner_url;?>" onclick="banner_click_count('<?=$banner_id;?>')"><img src=...>
When user clicks on image, it runs banner_click_count() function with $banner_id as parameter.
function banner_click_count($ban_id)
{
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {banner_id: $ban_id}
});
}
At banner_click.php, I get the banner_id with $banner_id = $_GET['banner_id']);, search the database based on it. Find the record, then add 1 to banner_count column field. After that, redirect to banner_url.
When I run the program, I get Parse error: parse error, expecting T_VARIABLE' or '$'' on line $.ajax({
Addendum: the error is cleared with all your help, but when I click on the link it redirects to banner_url directly and does not run the AJAX function.
Addendum:I put the alert("hello"); at the top of ajax function and i got it. So it goes into function
1.You need to put your javascript function under <script> tag
2.you need to pass json string as post data
3.though you are passing your data as post so you will get this data in php as $_POST not $_GET
So change your function as below
<script>
function banner_click_count(ban_id)
{
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {banner_id: ban_id}
});
}
</script>
// in your php use as below
echo $_POST['banner_id']
Make sure banner_id is in quotes and that you are including JQuery in your page.
And don't forget a success/error return.
$.ajax({
type: "POST",
url: 'banner_click.php',
data: {'banner_id': $ban_id},
success: function(s) {
console.log('success' + s);
},
error: function(e) {
console.log('error' + e);
}
});
Don't we need a return false before the function ends?
I found the solution. Thanks to all.
function banner_click_count(ban_id)
{
$.post(
"banner_click.php",
{
banner_id: ban_id
});
}
This is my JavaScript function which I have included in HEAD tag:
function test()
{
//enter code here
$.ajax({
url: 'test?msgto='+document.getElementById('Select1').value,
dataType: 'json',
type : 'GET',
success: function(result)
{
// console.log(result);
alert('test1');
// alert(string(result[0]['Select1']) );
// alert(result[0]['TextArea1']);
//document.getElementById('Text1').Value = ;// string(result[0]['location']);
}
});
}
and I want to send data to my PHP controller using this function on Cilck event of a button. I have written the following code for GETACTION()
// TODO Auto-generated method stub
//echo('Get');
$msgfrom = 1; //$this->_getparam('usrid');
$msgto = $this->_getparam('msgto');
$sql = "call select_messages(".$msgfrom.",".$msgto.");";
$data = Zend_Db_Table::getDefaultAdapter()->query($sql)->fetchAll();
$this->_helper->json($data);
But while on clicking I am not getting any output or result....
Kindly Guide me as soon as possible..... :)
Several misuses there.
Firstly, are you sure GETACTION() retrieves the /test path?
Secondly, in your JS code, I'm not sure you are calling the right path.
You'd better use this :
$.ajax({
type: 'GET',
url: '/test', // Notice the '/' at the beginning
data: {
msgto: $('#Select1').val(), // Since you're using jQuery, do it all the way :)
}
success: function(result) {
alert(result);
}
});
Notice the slash at the beginning of the url parameter. It means: "starting after the domain name". Also, you don't need the use the verbose document.getElementById() when you're using jQuery :).
Lastly, see the data parameter. jQuery will automatically build the correct URL before sending the AJAX request. I think it keeps your code cleaner.
I have the following ajax call which executes successfully:
function fnFormatDetails ( oTable, nTr )
{
var aData = oTable.fnGetData( nTr );
var memberid = 'memberid='+ aData[6];
$.ajax({
type: "POST",
url: "shout.php",
data: memberid,
success: function(html) {
//$("#shout").html(html);
var sOut = html.returned_val;
}
});
return sOut;
}
If I remove the commented out line ($("shout").html(html) and use a div on my page, the results display fine. However, there is a second function which would use the HTML results from sOut and display accordingly in the proper position.
The PHP file in shout.php simply 'echos' HTML to the page (which is then returned and displayed accordingly.
I am unfortunately unable to set the variable sOut currently based on the results of my ajax call. What am I missing?
If you'd like your function to return what returns from the AJAX call, you need to make the call synchronously. Also, this is assuming the result of "shout.php" is plaintext. If it's JSON or otherwise, you need to set the dataType property in your call to $.ajax.
function fnFormatDetails ( oTable, nTr ) {
var aData = oTable.fnGetData( nTr );
var memberid = 'memberid='+ aData[6];
var result;
$.ajax({
type: "POST",
url: "shout.php",
data: memberid,
async: false,
success: function(data) {
result = data;
}
});
return result;
}
First of all you are defining sOut within a success callback method and therefore it would not be available to fnFormatDetails.
Secondly by default $.ajax works in async mode, therefore whenever "return sOut" is called, the success callback would have not executed !
I would suggest you to call some method from the success which would do the process based on the html.returned_val, or you can pass async:false in $.ajax to make that call synchronized.
thank you #Matt Huggins
async: false
this small code change, fixed the issue i was having since morning. :)