Trying to run a local PHP file from a Javascript, Both locally - php

I have a Javascript that calls a PHP script using AJAX get method. If i run the PHP script externally it works fine and creates the file connections.txt. But with JS it is not working
$(document).on("click", "#target2", function(){
var arr = ["one","two","three"];
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
// here is the code that will run on client side after running clear.php on server
// function below reloads current page
alert(msg);
}
});
});
PHP script :
<?php
$fp = fopen('/Users/saurabh/Desktop/connections.txt', 'w');
echo "Saving file";
fwrite($fp, "hello");
//echo $_POST['yourarray']);
fclose($fp);
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
alert(msg);
}
});
});
</script>
I just ran that on my localhost, and it works fine. You must be making some mistake with the onclick function. Do it something like this...
$(document).ready(function(){
$("#someButton").click(function(){
$.ajax({
type: 'POST',
url: 'hello.php',
data: { name: "saurabh" },
success : function(msg) {
alert(msg);
}
});
});
});

Related

Can't figure out why PHP file doesn't get called by Ajax

this is my Code inside a html file:
<script>
$(document).ready(function (event)
{
$(document).on("click", ".interestbutton", function(e)
{
var docID = e.target.parentNode.parentNode.id;
$.ajax({
type: "POST",
url: "interest.php",
data: {docID },
success: function(data) {
console.log("done");
}
});
});
});
</script>
The "console.log" works but nothing inside my php file does. Any ideas?
Best regards
Change data: {docID } for data: {docID: docID} as it is an object.

How to echo an ajax variable in PHP file

I am trying to send a variable from one PHP file to another PHP file and echo it:
<script>
$.ajax({
type: "POST",
url: 'AjaxCallTest.php',
data: ({Imgname:"13"}),
success: function() {
alert('form was submitted');
}
});
<script>
And then in the receiving PHP file (AjaxCallTest.php):
<?php
$temp = $_POST['Imgname'];
echo $temp;
?>
but AjaxCallTest file doesn't echo anything? I need to get this variable and echo it. Note that I've included jQuery library in my code but I didn't include it in AjaxCallTest.php.
replace this line data: ({Imgname:"13"}), with data: {"Imgname":"13"}
I tried this and it worked! Notice the closing <script> tag
I also added the return result to check the echo in 'AjaxCallTest.php'
<script>
$.ajax({
type: "POST",
url: 'AjaxCallTest.php',
data: ({Imgname:"13"}),
success: function(result) {
alert(result + ' form was submitted');
}
});
</script>
Your code worked fine for me. One error I could find is that you didnt closed the script tag correctly. close the script tag.
<script>
$.ajax({
type: "POST",
url: 'AjaxCallTest.php',
data: ({Imgname:"13"}),
success: function() {
alert('form was submitted');
}
});
<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);
}
});
});

sending data using ajax, blank response coming from Server Side PHP

I am sending Data to PHP using Ajax.
But blank result receiving.
Client Side Code
<script>
//on click of a button, Below jQuery Ajax code running.
var prodStock= $("#product").html();
$.ajax({
data: prodStock,
url: url,
type: 'POST',
async: false,
success: function(response) {
if (response) {
alert(response);
}
}
});
</script>
<?php
foreach($data['Products'] as $val){
$pro.= $val['product_id'].':'.$val['quantity'].',';
}
?>
<div id='product'>{<?php echo rtrim($pro, ',');?>}</div>
Browser Giving:
<div id="product">{18:1,10:1}</div>
Server Side Code
public function update(){
if ($this->RequestHandler->isAjax()) {
print_r($this->request->data);
}
}
Testing:
If i am sending statically data: {18:1,20:1}, in ajax then working fine. returning Array.
Alternate Solution which i have tried and success.
Client Side Code:
<script>
//on click of a button, Below jQuery Ajax code running.
<?php
foreach($data['Products'] as $val){
$pro.= $val['product_id'].':'.$val['quantity'].',';
}
?>
var prodStock= $("#product").html();
$.ajax({
data: <?php echo "{".rtrim($pro, ',')."}";?>,
url: url,
type: 'POST',
async: false,
success: function(response) {
if (response) {
alert(response);
}
}
});
</script>

How to load php script into div with jquery ajax?

$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
$("#p1").text(data);
$("#tempForm").load('tv_form.php');
}
});
});
});
It is my code , in load() function when I call 'tv_form.html' file it works, but when i call 'tv_form.php' it doesn't work , error is 403 forbidden
where is the problem ? .html works, but .php doesn't work.
Add URL instead of file name.
$(document).ready(function(){
$("#S1").change(function()
{
var IDCat=this.value;
$.ajax({
type: "GET",
url: 'product_modify.php',
data: {IDCat:IDCat},
success: function(data)
{
$("#p1").text(data);
$("#tempForm").load('URL to tv_form.php'); // www.example.com/file.php
}
});
});
});

Categories