Ajax PHP: Variable from page url - php

With the following AJAX call I set pagination for a webpage. It works.
In my PHP file already have:
$page= $_POST[page];
AJAX call:
function pg2(page) {
pag.ajax({
type: "POST",
url: "file.php",
data: { page: page },
success: function(ccc) {
pag("#search_results").html(ccc);
}
});
}
I also need to pass id from the URL.
http://website.com/title/?id=2 **//I need to pass id in php file and echo it out.
How can I do that? many thanks.

var id=<?php echo $_GET['id'];?> // like this you can store php variable in javascript varibale
Now call function pg2(page,id) however you want...
function pg2(page, id) {
pag.ajax({
type: "POST",
url: "file.php",
data: { page: page, id: id },
success: function(ccc) {
pag("#search_results").html(ccc);
}
});
}
hope it may help you

If your JS is embedded:
function pg2(page) {
var id = <?php echo intval($_GET['id']); ?>;
pag.ajax({
type: "POST",
url: "file.php",
data: { page: page, id: id },
success: function(ccc) {
pag("#search_results").html(ccc);
}
});
}
If your JS is in an external file (best option):
var id = <?php echo intval($_GET['id']); ?>;
pg2(page, id);

Read id via GET and pass in the function
$id = $_GET['id'];
function pg2(page, id) {
pag.ajax({
type: "POST",
url: "file.php",
data: { page: page, id: id },
success: function(ccc) {
pag("#search_results").html(ccc);
}
});
}

Related

Ajax send javascript variable to php

I am trying to send a JS variable to a PHP file but it does not seem to be working. In the console its showing an error as an show.php file. I am not able to understand What I am doing wrong.
function details(id) {
var id = id;
// alert(id);
$.ajax({
type: 'POST',
url: 'show.php',
data: id,
success: function(data) {
alert("hi");
}
});
}
<button onclick="details(<?php echo $id ; ?>)" class="btn btn-rounded btn-primary">Details</button>
show.php:
<?php
if (isset($_POST['id']))
{
$uid = $_POST['id'];
echo json_encode($uid);
}
?>
Write data in json object
see code below
function details(id)
{
var id = id;
// alert(id);
$.ajax({
type: 'POST',
url: 'show.php',
data: {id:id},
success: function(data)
{
alert("hi");
}
});
}
Check your network tab and check the sending parameters list . You have to mention datatype json
Try this
function details(id)
{
var id = id;
// alert(id);
$.ajax({
type: 'POST',
url: 'show.php',
dataType: 'json',
data: {'id':id},
success: function(data)
{
alert("hi");
}
});
}
In your show.php
<?php
if (isset($_POST['id']))
{
$uid = $_POST['id'];
echo json_encode($uid);
}
?>

How can access base Site URL id into ajax and pass to controller using Codeigniter?

I am new in Codeigniter
I want to access base URL ID into AJAX, within I also pass another id to the controller in Codeigniter. At the controller side, I access two different id one site_url id and another s_id.
So how can I achieve this task?
Site URL
https://www.demoexample.com=?uid=10
Ajax script
<script>
$(document).ready(function(){
$('#member_type').on('change',function(){
var memId = $(this).val();
if(memId!=''){
$.ajax({
url: "<?php echo site_url('index.php/template/vehicle/get_member_type')?>",
method:'POST',
data:'member_id='+memId, // here I want to access site_url id and s_id from drop-down id
success:function(data)
{
$('#mid').html(data);
}
});
}
else{
$('#mid').html('<option value="">Select sttxt_Standard first</option>');
}
});
});
</script>
Controller .php file
function show_vehicle_reg(){
$id=$this->input->post('id'); // access multiple id over here from ajax
}
}
Thanks in advance
ajax:
GET Method
$.ajax({
beforeSend: function () {
},
complete: function () {
},
type: "GET",
url: "<?php echo site_url('controller/cmethod/'.$s_id); ?>",
success: function (data) {
}
});
POST Method
$.ajax({
beforeSend: function () {
},
complete: function () {
},
type: "POST",
url: "<?php echo site_url('controller/cmethod'); ?>",
data: ({member_id: memId}),
success: function (data) {
}
});

PHP variable not echoing on page via ajax jquery

I am having a problem with my code as my php variable is not being echoed on page when using ajax jquery. Here is my code...
<script>
function refresh_div() {
$('.loader').show();
var username = "<?php echo $user; ?>";
jQuery.ajax({
type: "POST",
url: "load.php",
data:{user : username},
success:function() {
jQuery("#load_msgs").append(response+'<br>');
},
complete: function(){
$('.loader').hide();
}
});
}
t = setInterval(refresh_div,1000);
</script>
i am trying to send "username" to page url "load.php"... i called it this way but nothing is being echoed...
if(isset($_POST['user'])) {
echo $_POST['user'];
}
pls help out thanks... :)
edited...
when i tried using this code i.e adding passing response as parameter in success function like this ...
<script>
function refresh_div() {
$('.loader').show();
var username = "<?php echo $user; ?>";
jQuery.ajax({
type: "POST",
url: "load.php",
data:{user : username},
success:function() {
jQuery("#load_msgs").append(response+'<br>');
},
complete: function(){
$('.loader').hide();
}
});
}
t = setInterval(refresh_div,1000);
</script>
.... the data (username) gets displayed every second.. like a blink toggling on and off the page... how do i make the data display static in order to use the variable on the ajax page. Thanks :)
Where the response variable you get? Should be :
success : function( response ) {
jQuery("#load_msgs").append(response+'<br>');
},
please take a datatype as json in ajax call
and in loads.php function add two line as below..
if(isset($_POST['user'])) {
$data['user'] = $_POST['user'];
}
echo json_encode($data);
also change in success function.
success:function(response) {
jQuery("#load_msgs").append(response.user);
}
A bit more simplified would be:
$.post(URL, { data: somefield }, function(result) {
result = JSON.parse(result);
If(result && result.status === 'true'){
}
)};
Depends a bit on the return at server side.
You missed response in success function.
Try this:
<script>
function refresh_div() {
$('.loader').show();
var username = "<?php echo $user; ?>";
jQuery.ajax({
type: "POST",
url: "load.php",
data:{user : username},
success:function(response) {
$("#load_msgs").append(response+'<br>');
},
complete: function(){
$('.loader').hide();
}
});
}
t = setInterval(refresh_div,1000);
</script>

Get the content of json file on page load

I am using ajax, php and json to store click counts in json file...Let's say the file is this:
count.json
{"countbtn1":28,"countbtn2":25,"countbtn3":39}
downloadsCount.php
<?php
$buttonName=$_POST["buttonName"];
$file = "count.json";
$json = json_decode(file_get_contents($file), true);
echo $json['count'.$buttonName] ;
?>
downloads.php
<?php
$buttonName=$_POST["buttonName"];
$file = "downloads.json";
$json = json_decode(file_get_contents($file), true);
$json['count'.$buttonName] = $json['count'.$buttonName] + 1;
file_put_contents($file, json_encode($json));
echo 'success';
?>
And I need to place each btn value inside these on page load:
<div class='smallbtn1'>0</div>
<div class='smallbtn2'>0</div>
<div class='smallbtn3'>0</div>
Counting and updating the json file with this:
$('.download').click(function() {
//get name of button
var name= $(this).prop('name');
$.ajax({
url: "downloads.php",
data:{buttonName:name},
method: "POST",
success: function(response) {
if (response = 'success') {
//get count download
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response);
}
});
}
}
});
return true;
});
..and I tried updating the count on page load with this:
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response); }
});
});
...but it doesn't update the values on load, just after the first click.
PS: The whole thing IS wrapped in $(document).ready(function(){..});
After seeing the code on your server, I'd say the issue is how you are calling the AJAX. You are doing this:
$(document).ready(function(){
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name},
method: "POST",
success: function(response){
$('.small'+name).html(response); }
});
});
But you don't have defined what name is (it is defined on the click event, but not when the page is loaded for the first time). You should do something like this:
$(".download").each(function() {
var name = $(this).attr("name");
$.ajax({
url: "downloadsCount.php",
data:{buttonName:name },
method: "POST",
success: function(response){
$('.small'+name).html(response);
}
});
});
So the AJAX is called for each one of the buttons, and you use the attribute name by doing $(this).attr("name").
Try sending the AJAX request after the page completely loads using $(document).ready():
$(document).ready(function() {
$.ajax({
url: "downloadsCount.php",
data: { buttonName:name },
async: false,
method: "POST",
success: function(response) {
$('.small' + name).html(response);
}
});
});

recieving header(location:) as a callback from a php script in jquery .ajax

With this jquery :
$.ajax({
type: "POST",
url: "tosql.php",
data: {
content: content
},
success: function() {
}
});
I was wondering if I could receive a header(location: ) from the php, to redirect to another page with specific values stored in the url. Here is the url: header(location: 'morefive.php?document='.urlencode($id))
Assuming tosql.php returns id you could do this :
$.ajax({
type: "POST",
url: "tosql.php",
data: {
content: content
},
success: function(data){
window.location = 'morefive.php?id=' + data;
}
});
Update :
On tosql.php :
$_POST['content'];
// do something with content
echo $id; //say $id = 123
You would be directed to morefive.php?id=123 on success.

Categories