Page content won't load and Uncaught SyntaxError: Unexpected token ; - javascript

Hi some part of our site just keeps loading and when I try to check via Inspect element I have a error of Uncaught SyntaxError: Unexpected token ;
and as per checking i have problem with this code
<script>
<?php echo "var place =".$city.";"; ?>
jQuery(document).ready(function(){
//console.log(place);
getLocation();
//get_locations(place.city,'<?php echo BASE_URL.'clinics/get_near_locations'; ?>');
});
function getLocationByCity(data){
var city = place.city;
var url = '<?php echo BASE_URL.'clinics/get_near_locations'; ?>';
// if(city!='false')
// jQuery('#current-city').html('Current City: '+city);
jQuery.ajax({
url:url,
type: 'POST',
dataType: 'html',
data:'city='+city,
beforeSend: function(){
jQuery('.button').html('<span>Please wait...</span>');
},
success: function(data){
jQuery('#clinic-locations').html(data);
}
});
}
function getLocation()
{
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(getLocationByLatLong,errorHandler,{timeout:6000});
}else{
getLocationByCity();
}
}
function errorHandler(err)
{
console.log(err);
console.log('errorHandler');
getLocationByCity();
}
function getLocationByLatLong(position)
{
jQuery.ajax({
url:'<?php echo BASE_URL.'clinics/get_near_locations'; ?>',
type: 'POST',
dataType: 'html',
data:'latitude='+position.coords.latitude+'&longitude='+ position.coords.longitude,
beforeSend: function(){
jQuery('.button').html('<span>Please wait...</span>');
},
success: function(data){
jQuery('#clinic-locations').html(data);
}
});
}
I also try to remove ; from 2nd line but no luck.
Anyone can help? Thanks!

Your problem is in the first row:
<?php echo "var place =".$city.";"; ?>
Strings should be around quotes or apostrophes, so change it to
<?php echo "var place = '$city';"; ?>

Related

$.ajax an't figure out why PHP not receiving POST data from $.ajax call

$.ajax successed,but in the php, I cannot get the data from ajax.
Notice: Undefined index: email in..
Notice: Undefined index: pass in ..
$(document).ready(function(){
$(document).on('click',"#bolero-user-login",function(){
var email = $("#edit-name").val();
var pass = $("#edit-pass").val();
// alert (email);
$.ajax({
url:"functions/php/login.php",
type:"POST",
data:{ "email":email,"pass":pass},
beforeSend: function () {
},
success:function(){
alert("success");
alert(email);
$("#edit-name").val(" ");
$("#edit-pass").val(" ");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
},
});
return false;
});
});
PHP:
<?php
define ("root",$_SERVER['DOCUMENT_ROOT']);
// echo root;
include root.'/maroon5/includes/dbh.php';
print_r($_REQUEST);
$email = $_POST['email'];
$pwd = $_POST['pass'];
?>
HERE is the pictures from firefox
Image
Image
Change AJAX success into
success:function(data){
console.log(data);
$("#edit-name").val(" ");
$("#edit-pass").val(" ");
},
And AJAX page as well
<?php
define ("root",$_SERVER['DOCUMENT_ROOT']);
// echo root;
include root.'/maroon5/includes/dbh.php';
// print_r($_REQUEST);
$returnArr['email'] = $email;
$returnArr['pwd'] = $pwd;
echo json_encode($returnArr);
?>

ajax post to php a null variable

I have a script that copies an entire div into a variable. It works when I alert the data, but It wont work when I try to echo it in php.
<script>
var vin = "<?php echo trim($vin1); ?>";
function orderImage(){
var orderIm=$('<div/>').append($('#image-dropzone').clone()).html();
$.ajax({
type: 'POST',
url: 'orderImage.php?id='+vin,
data: {ordering:orderIm},
dataType: 'html'
})};
</script>
And my php:
<?php
echo $_GET['id'];
echo '<br />';
echo gettype($_POST['ordering']);
echo $_POST['ordering'];
?>
Output:
JS2YB417785105302
NULL
You can using full post request
<script>
var vin = "<?php echo trim($vin1); ?>";
function orderImage(){
var orderIm=$('<div/>').append($('#image-dropzone').clone()).html();
$.ajax({
type: 'POST',
url: 'orderImage.php,
data: {ordering:orderIm,id:vin}, // added id:vin on POST parameter
dataType: 'html'
})};
</script>
And my php :
<?php
echo $_POST['id']; // converted into $_POST
echo '<br />';
echo gettype($_POST['ordering']);
echo $_POST['ordering'];
?>
when i use the succes function :
var vin = "<?php echo trim($vin1); ?>";
function orderImage(){
var orderIm=$('<div/>').append($('#image-dropzone').clone()).html();
$.ajax({
type: 'POST',
url: 'orderImage.php',
data: {ordering:orderIm,id:vin},
dataType: 'html',
success: function(data) {
alert(data)}
})};
it shows the correct data. i guess it means that its solved. i just dont see it in the php file while i access it via the console log--> double clicking on XHR finished loading.
even tho the variables types shows NULL in the php script, i can still manipulate the content of them and everything is working fine !

Wordpress AJAX doesn't work - response 0

I want add AJAX support to my plugin and I have huge problem with this simple thing. WordPress isn't permitting me to use normal AJAX and I need to use WordPress version.
At all times, the WordPress function (that should generate output) returns 0. And I think that the reason is that WP doesn't trigger 'function'. I try to force the function to run many times, but I don't have any idea what I can improve.
<?php
public function widget( $args, $instance ) {
$options = get_option('Free_Quotation_options');
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
jQuery.ajax({
url: ajaxurl,
type: 'POST',
action: 'fqtag',
data: {
'whatever': 'text'
},
success: function (output) {
$('#secondary').append(output);
}
});
});
</script>
<?php
add_action( 'wp_ajax_fqtag', 'fqtag' );
add_action( 'wp_ajax_nopriv_fqtag', 'fqtag' );
function fqtag() {
global $wpdb;
echo 'echo';
die();
}
}
I try to add alert('echo'); to the test function, but it doesn't have any effect. I think that AJAX doesn't run the proper function: fq_tag_support_callback() .
On the beginning I had a problem with ajaxurl variable. It was not defined. It's not a normal situation. I attempt to resolve this problem by using:
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
Do you have any idea, how can I try to solve this problem?
---EDIT--- After discussion with David I have file like this (all time doesn't work)
<?php
/*
Plugin Name: TEST PLUGIN
Description: TEST
Author: Krzysztof Kubiak
Version: 1.0
*/
function Test_01_settings_init(){
register_setting( 'Test_01_settings_filed', 'Test_01_options', 'Test_01_validate' );
}
add_action('admin_init', 'Test_01_settings_init' );
function T01_init_method() {
wp_enqueue_script('jquery');
}
add_action('init', 'T01_init_method');
function Test_01_menu_page(){
add_menu_page( 'Test_01', 'Test_01', 'manage_options', 'T01_menu_page', 'T01_add_page' );
echo my_test();
}
add_action('admin_menu', 'Test_01_menu_page');
function my_test(){
echo 'Function test';
}
function T01_add_page() {
echo 'TEST_01_plugin';
}
function Test_01_validate($input) {
}
//AJAX FROM HIRE
function test_callback() {
$whatever = 8;
echo $whatever;
die();
}
add_action( 'wp_ajax_nopriv_fqtag', 'test_callback', 1 );
add_action( 'wp_ajax_fqtag', 'test_callback', 1 );
function print_js() { ?>
<script type="text/javascript">
jQuery.ajax({
url: 'wp-admin/admin-ajax.php',
type: 'POST',
action: 'fqtag',
data: {
'whatever': 'text'
},
success: function (output) {
alert(output);
}
});
</script>
<?php
}
add_action('wp_print_footer_scripts', 'print_js', 1000);
?>
remove
<script>alert('echo');</script>
your response should be echo if you check your console. I suspect all the above code is in your plugin functions file. Basically the php function should be placed in the functions file.
The jquery should be placed in the template from which you want to receive the response.
Place this in your functions file...remove the jquery from the class...
add_action('wp_print_footer_scripts', 'print_js', 1000);
function print_js() { ?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery.ajax({
url: 'wp-admin/admin-ajax.php',
type: 'POST',
data: {
'action': 'test_callback',
'whatever': 'text'
},
success: function (output) {
alert(output);
}
});
});
</script>
<?php
}
Move this outside your class...
function test_callback() {
$whatever = 8;
echo $whatever;
die();
}
add_action( 'wp_ajax_nopriv_testaction', 'test_callback' );
add_action( 'wp_ajax_testaction', 'test_callback' );
Just make sure that you have put the function 'fq_tag_support_callback()' in your plugin's main file.
I see few problems here. Action should inside the data object, not as a jQuery Ajax parameter. Also in the callback function data is stored in $_POST variable.
function test_callback() {
$whatever = $_POST['whatever'];
echo $whatever;
die();
}
add_action('wp_ajax_nopriv_fqtag', 'test_callback');
add_action('wp_ajax_fqtag', 'test_callback');
function print_js() {
?>
<script type="text/javascript">
jQuery.ajax({
url: <?php echo admin_url('admin-ajax.php'); ?>,
type: 'POST',
data: {
action: 'fqtag',
whatever: 'text'
},
success: function (output) {
alert(output);
}
});
</script>
<?php
}
add_action('wp_print_footer_scripts', 'print_js', 1000);
?>

Facing on Make Like Unlike Button in php , mysql using ajax calling

I m creating like and unlike button for my website , at my local server i used seprate file it it working fine but when i install at my website then it is not working properly.
I m Fetching some information using this code from url bar
<?php
error_reporting (0);
include "includes/session.php";
include 'database/db.php';
extract($_REQUEST);
if(isset($_GET["i"]))
{
$pid=($_GET['p']);
$lid=($_GET['i']);
}
?>
<?php
$likquery = mysql_query("select * from likes where userid=$id and postid=$lid");
if(mysql_num_rows($likquery)==1){
?>
<a class="unlike" id="<?php echo $lid ?>">UnLike</a> <span class="like_update" id="<?php echo $lid ?>"></span>
<?php }else{?>
<a class="like" id="<?php echo $lid ?>">Like</a>
<?php
}?>
AFTER this I use script
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('body').on('click','.like',function(){
var postId = $(this).attr('id');
var postData = 'postid='+postId+'&uid=<?php echo $id ?>';
$.ajax({
type: "POST",
url: "like_notes",
data: postData,
cache: false,
success: function(){
$('#'+postId).text('UnLike').addClass('unlike').removeClass('like');
}
});
})
$('body').on('click','.unlike',function(){
var postId = $(this).attr('id');
var postData = 'postid='+postId+'&uid=<?php echo $id ?>';
$.ajax({
type: "POST",
url: "likeun_notes",
data: postData,
cache: false,
success: function(){
$('#'+postId).text('Like').addClass('like').removeClass('unlike');
}
});
})
});
</script>
And I m not getting any response but when i use
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$(".like").click(function(){
var postId = $(this).attr('id');
var postData = 'postid='+postId+'&uid=<?php echo $id ?>';
$.ajax({
type: "POST",
url: "like_notes",
data: postData,
cache: false,
success: function(){
$('#'+postId).text('UnLike').addClass('unlike').removeClass('like');
}
});
})
$(".unlike").click(function(){
var postId = $(this).attr('id');
var postData = 'postid='+postId+'&uid=<?php echo $id ?>';
$.ajax({
type: "POST",
url: "likeun_notes",
data: postData,
cache: false,
success: function(){
$('#'+postId).text('Like').addClass('like').removeClass('unlike');
}
});
})
});
</script>
only once it is changing the value
Ok, the second version works only once, because at the beginning, the event is on the button, then you re-render the button by changing it's class and text, so the events get unbinded.
Same is for the 1st version. Jquery says: Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on().... If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page.
So re-bind the events after changing the button.
I suggest something like this:
function bindEvents() {
$('.like').off().on('click', function(){
var postId = $(this).attr('id');
var postData = 'postid='+postId+'&uid=' + <?php echo $id ?>;
$.ajax({
type: "POST",
url: "like_notes",
data: postData,
cache: false,
success: function(){
$('#'+postId).text('UnLike').addClass('unlike').removeClass('like');
//rebind events here
bindEvents();
}
});
$('.unlike').off().on('click', function(){
var postId = $(this).attr('id');
var postData = 'postid='+postId+'&uid=' + <?php echo $id ?>;
$.ajax({
type: "POST",
url: "likeun_notes",
data: postData,
cache: false,
success: function(){
$('#'+postId).text('Like').addClass('like').removeClass('unlike');
//rebind events here
bindEvents();
}
});
}
$(document).ready(function(){
bindEvents();
});
Also notice that in var postData = 'postid='+postId+'&uid=<?php echo $id ?>'; code, the php part won't be treated as php. It will be just a string. So replace that part with the following, assuming that the code is located inside a php file:
var postData = 'postid='+postId+'&uid=' + <?php echo $id ?>;

How to pass variables from PHP to Javascript using Ajax calls

I read this post and assumed the technique in the answer would work with ajax calls. I have my ajax and php code below but it does not work.The client does not recognize the 'passed' variable. I do not know why nor how to remedy this.
Javascript
var irrelevant = 'irrelevant';
$('body').click(function(){
$.ajax({
type: 'POST',
url: 'test.php',
data: {mydata: irrelevant},
success: function(){
console.log('worky');
alert(myvar); // NOT worky!
}
});
});
PHP File
<?php
$thing = 10;
?>
<script>
var myvar = "<?php echo $thing; ?>";
</script>
try this in your ajax.success
success: function(data){
console.log('worky');
alert(data); // It should now, worky!
}
and in you php
<?php
echo 10;
?>
try this
in php
<?php $thing = 10; ?>
<script>
var myvar = "<?php echo $thing; ?>";
</script>
javascript
$('body').click(function(){
$.ajax({
type: 'POST',
url: 'test.php',
data: {mydata: irrelevant},
success: function(data){
$("#hiddendiv").html(data);
alert(myvar);
}
});
});

Categories