i'm new to web and facing issue while using PHP file's constants inside javascript Ajax call.
My PHP code in constants.php file is as:
<?php
$color = 'green';
define ('BASE_URL', 'https://example.com?');
define ('APP_KEY', 'abcde');
define ('USER_KEY', '12345');
?>
My Ajax call in another login.php file is as:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#loginPopup").on('click',function(){
var x = document.forms["login"]["emailId"].value;
var pwd = document.forms["login"]["pwd"].value;
$.ajax({
type: 'GET',
url: 'BaseURL?appkey=abcde&userkey=12345&email='+ x +'&password=' + pwd,
crossDomain: true,
dataType: 'jsonp',
success: function (response) {
showAlert(response);
},
error: function (request, status, error) {
alert("ERROR");
}
});
});
});
</script>
I want to move constants i.e. Base URL, Keys etc to constants file. So i've created constants.php. But now, i don't know how to use that inside ajax call. Please help. Thanks in anticipation.
Like so, assuming the two files are in the same dir:
<?php
require_once('constants.php');
?>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#loginPopup").on('click',function(){
var x = document.forms["login"]["emailId"].value;
var pwd = document.forms["login"]["pwd"].value;
$.ajax({
type: 'GET',
url: '<?php echo BASE_URL ?>?appkey=<?php echo APP_KEY ?>&userkey=<?php echo USER_KEY ?>&email='+ x +'&password=' + pwd,
crossDomain: true,
dataType: 'jsonp',
success: function (response) {
showAlert(response);
},
error: function (request, status, error) {
alert("ERROR");
}
});
});
});
</script>
Related
i have link in my page
+1111
i want to save every click in database
i made this ajax code :
<script type="text/javascript">
$('#tel').on('click', function() {
var location = $(this).attr('href');
var action = 'script.php';
$.ajax({
method: 'POST',
url: action,
data: '',
dataType: 'json',
success: function (data) {
window.location = location;
},
error: function(data){
return false;
}
});
});
</script>
now what must i do, what is the code i have to put in script.php
First you need send some data to script.php, for example
url: action,
data: {location: location},
dataType: 'json',
next in script.php you can read this data
$_POST['location']
create sql query and save data in DB.
send href as json
<script type="text/javascript">
$('#tel').on('click', function() {
var location = $(this).attr('href');
var action = 'script.php';
$.ajax({
method: 'POST',
url: action,
data: {"href":location}, // this line update
dataType: 'json',
success: function (data) {
console.log(data['state]); // this line add for debug server side
window.location = location;
},
error: function(data){
return false;
}
});
});
</script>
and update script.php like below:
<?php
if($_POST['href']){
//connect to database
//update table
echo json_encode(array('state'=>'ok'));
}else{
echo json_encode(array('state'=>'error'));
} ?>
I am trying to test AJAX in CodeIgniter, with no luck so far. Please tell me where I am going wrong.
Here is my test_page.php:
<!DOCTYPE html>
<head>
<script src="<?php echo base_url();?>assets/libs/jquery/jquery-2.2.3.min.js"></script>
<script src="<?php echo base_url();?>assets/libs/jquery/jquery-2.2.3.min.js"></script>
<script src="<?php echo base_url();?>assets/libs/js-cookie/js.cookie.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<button id="btn" onclick="gethtml()">ajax Test</button>
<script type="text/javascript">
function gethtml(){
var url = "<?php echo base_url();?>home/ajax_test";
alert(""+url);
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
success: function(data){
alert("ajax success");
}
});
}
</script>
</body>
</html>
Here is my function in controller:
public function ajax_test() {
echo "return from ajax";
}
This Type of AJAX Call dataType is not required
Remove
dataType: 'json'
Use like
<script type="text/javascript">
function gethtml(){
var url = "<?php echo base_url();?>home/ajax_test";
alert(""+url);
$.ajax({
url: url,
type: 'POST',
success: function(data){
alert("ajax success");
}
});
}
</script>
You can use below mentioned solution.
<button id="btn">ajax Test</button>
$(document).ready(function(){
$('#btn').Click(function(e){
e.preventDefault();
var url = "<?php echo base_url();?>home/ajax_test";
alert(""+url);
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
success: function(data){
alert("ajax success");
}
});
});
});
Let me know if it not works for you.
Error in the above code is the response from the ajax_test() is not encoded into json. So, the error event of json is triggered and the code inside success is not executed.
Always make sure to send json encoded data if you are specifying dataType as json.
Try on view
<button id="btn">ajax Test</button>
<script type="text/javascript">
$('#btn').on('click', function(e) {
// e.preventDefault(); Not sure if you need it.
$.ajax({
url:"<?php echo base_url('home/ajax_test');?>",
type: 'POST',
dataType: 'json',
// data: {
// someinput: $('#someinput').val()
//},
success: function(data){
alert(data['test']);
}
});
});
</script>
On controller you can use the output class also.
public function ajax_test() {
$json['test'] = "return from ajax";
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode($json));
}
I Am trying to send value from ajax to php and retrieve it just to test that everything is work, when i click in a button to test i got error and alert('Failed') Appears , how can i fix it in order to get success? thanks
Ajax :
var a = "test";
$.ajax({
url: "search.php",
dataType: "json",
data: a ,
success: function(data) {
alert('Successfully');
},
error: function(data) {
alert('Failed');
}
})
PHP :
<?php
$pictures = "img1";
echo json_encode($pictures);
?>
I refined your code slightly and it works.
var a = "test";
$.ajax({
type: 'POST',
url: 'search.php',
data: 'a=' + a,
dataType: 'json',
cache: false,
success: function (result) {
alert('Successful');
},
error: function (result) {
alert('Failed');
}
});
If you're requesting a JSON, use the $.getJSON from jQuery, it's aready parse the JSON into a JSON object for you.
Seems that you're not return an actual JSON from server, maybe this is what is causing the error.
If you're seeing the 'Failed' message probably the problem is a 500 error which is a server error.
Try this code above.
Javascript:
var a = "test";
$.getJSON("search.php", {
a: a
}, function (json) {
console.log(json);
});
PHP:
<?php
$pictures = ["img1"];
echo json_encode($pictures);
The only way to this not work, is if you have a huge mistake on you webserver configuration.
Your ajax is wrong, it should be:
var a = "test";
$.ajax({
type: "POST",
url: "search.php",
dataType: "json",
data: {a:a},
success: function(data) {
alert('Successfully');
},
error: function(data) {
alert('Failed');
}
});
Hello I have a problem with wordpress I can not get an ajax call and I can not find the reason. My query returns me all the time 0.
my javascript code :
updateButton.onclick = function (e) {
var donne = {
'action': 'my_action',
'lodges': updateDeleteArray
};
$(function () {
$.ajax({
type: "POST",
data: donne,
url: ajaxurl,
contentType: "application/json",
dataType: 'json',
success: function (data) {
console.log(data);
},
});
});
};
my php code :
add_action('wp_ajax_my_action', 'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');
function my_action() {
echo 'salut';
die();
}
Try deleting contentType and dataType, because you are not returning a JSON into your function.
Or
modify your function like this
function my_action() {
echo json_encode('salut');
die();
}
hope it helps
I am using the following script
<script>
$(document).ready(function(){
$("#view").click(function(){
var requestId = $("#hdnRequestId").val();
$.ajax({
type: "POST",
url: "enquiryProcess.php",
data: requestId,
cache: false,
success: function(data){
console.log(data);
}
});
return false;
});
});
My controller function is
<?php
include('enquiry_function.php');
$functionObj=new Enquiry();
if(isset($_POST['requestId']))
{
$qt_request_id=$_POST['requestId'];
$responce=$functionObj->view_enquiry_request($qt_request_id);
echo json_encode($responce);
}
?>
And my model function is
class Enquiry
{
public function view_enquiry_request($qt_request_id)
{
$query=mysql_query("SELECT * FROM quote_request WHERE qt_request_id='$qt_request_id'");
$result=mysql_fetch_assoc($query);
return $result;
}
}
I did not get any error.But result in console message is empty.How to get the result from php in jquery ajax.please help me.
Please change
var requestId = $("#hdnRequestId").val();
$.ajax({
type: "POST"
, url: "enquiryProcess.php"
, data: {"requestId":requestId}
, cache: false
, success: function (data) {
console.log(data);
}
});
Pass data as PlainObject or String or Array. See jQuery documentation here http://api.jquery.com/jquery.ajax/