Cannot call ajax function in php - php

I want to call ajax from php(i put the function and the php at the same page) but i get error undefined index hash. please help me guys. this is my code:
<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-
FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
</body>
</html>
<?php
echo $_POST["hash"];
?>
<script type="text/javascript">
var hash = window.location.hash;
$.ajax({
url:"test.php",
method:"POST",
data:{hash:hash},
success: function (data)
{
}
});
</script>

You need to have 2 files, say index.html:
<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-
FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
</body>
</html>
<script type="text/javascript">
var hash = window.location.hash;
$.ajax({
url:"test.php",
method:"POST",
data:{hash:hash},
success: function (data)
{
}
});
</script>
and test.php:
<?php
echo $_POST["hash"];
?>

Here is an example test.php
<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-
FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
<script type="text/javascript">
var hash = window.location.href;
$.ajax({
url:"test1.php",
method:"POST",
data:{hash:hash},
success: function (data)
{
alert(data);
}
});
</script>
</body>
</html>
Here is the test1.php
<?php echo $_POST["hash"]; ?>

Related

Save file from ajax

I want to save the image generated on the server. For this I use iframe, but the File Save dialog does not appear after the click. What am I doing wrong?
index.php
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#onv-save-button').click( function() {
$.ajax ({
url: "/ajax.php",
type: "POST",
success: function(data) {
$('#downloadFrame').attr('src' , data);
}
});
});
});
</script>
</head>
<body>
<iframe src="" id="downloadFrame" ></iframe>
<button id="onv-save-button">Go!</button>
</body>
</html>
ajax.php
<?
// Some actions to generate image
echo "1.png" ;
?>
I did. Here is the answer to the question.
index.php
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#onv-save-button').click( function() {
$.ajax ({
url: "/ajax.php",
type: "POST",
success: function(data) {
alert(data);
$('#downloadFrame').attr('src' , "download.php?file=" + data);
}
});
});
});
</script>
</head>
<body>
<iframe src="" id="downloadFrame" ></iframe>
<button id="onv-save-button">Go!</button>
</body>
</html>
ajax.php
<?
echo $_SERVER["DOCUMENT_ROOT"]."/1.png";
?>
download.php
<?php
$content = file_get_contents($_REQUEST['file']);
header('Content-Description: File Transfer');
header("Cache-Control: ");
header("Pragma: ");
header("Content-Disposition: attachment; filename=\"".basename($_REQUEST['file'])."\"");
ob_end_clean();
ob_start();
echo $content;
ob_end_flush();
exit();
?>

Pass JQuery variables to php

I want to pass jQuery variable to php file ,
this is my_js.js file
function updates() {
$.getJSON("php/fetch.php", function(data) {
$.each(data.result, function(){
var s_id = this['sender_id'];
});
});
}
and i want to display it here in php file,
<html>
<head><title>Pass jquery var to php file</title>
</head>
<body>
<div> <?php echo $s_id ; ?> </div>
<script type="text/javascript" src="my_js.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</body>
</html>
You might consider using jQuery to set the s_id value in the page.
You can modify your HTML as follows (the important part being the added span element):
<html>
<head><title>Pass jquery var to php file</title>
</head>
<body>
<div><span id="s_id"></span></div>
<script type="text/javascript" src="my_js.js"></script>
<script type="text/javascript" src="jquery.js"></script>
</body>
</html>
And you can use jQuery in your getJSON callback to set the value as follows to find your added span element and modify its text:
function updates() {
$.getJSON("php/fetch.php", function(data) {
$.each(data.result, function(){
$("#s_id").text(this['sender_id']);
});
});
}

Simple Ajax request with php

I'm trying to pass a simple variable from my jQuery to my php and then echo it on my HTML. It doesn't seem to work though.
I can't get my variable $result to show up on my page. Any thoughts? The Ajax POST seems to be working fine. The problem seems to be communication between php and HTML.
index.html:
<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type='text/javascript' src='front.js'></script>
</head>
<body>
<?php echo $result; ?>
</body>
</html>
front.js:
$(document).ready(function() {
$.post('/back.php', {name: "Bob"}, function(data) {
});
});
back.php:
<?php $result = $_POST['name']; ?>
Try this
index.html:
<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type='text/javascript' src='front.js'></script>
</head>
<body>
</body>
</html>
front.js:
$(document).ready(function() {
$.post('/back.php', {name: "Bob"}, function(result) {
$('body').html(result);
});
});
back.php:
<?php echo $_POST['name']; ?>
Change this
<body>
<div id="show-data"></div>
</body>
And
$(document).ready(function() {
$.post('/back.php', {name: "Bob"}, function(data) {
$('#show-data').html(data);
});
});
And
<?php echo $result = $_POST['name']; ?>
Change Your PHP Code :
<?php echo $_POST['name']; ?>
And Also Your HTML Code :
<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
</body>
<script>
$(document).ready(function() {
$.post('/back.php', {name: "Bob"}, function(data) {
$('body').append(data)});
});
</script>
</html>
$result in back.php and $result in index.html aren't related. Setting it in back.php has no effect on index.html. If you echo something in back.php however, that output will be passed to the empty callback function you have in your $.post() call. You can handle the data there and use javascript to insert it into your page.
Just add the following line to your code: $('body').html(data);
Like your question we have three different files one is index.html
1) index.html
<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type='text/javascript' src='front.js'></script>
</head>
<body>
<?php echo $result; ?>
</body>
</html>
2) front.js
$(document).load(function(){
$.ajax({
url: '/back.php',
type: 'POST',
data: 'name=Bob',
async: false,
success: function(data) {
$('body').html(data);
},
cache: false
});
});
3) back.php
<?php echo $result = $_POST['name']; ?>
Now use this, your files will work out. Please let me know if you have any queries.
Puh, lot of mistakes. First, your index.html contains PHP - Code which won't be executed because this is not a PHP - Skript (ending with php).
Second you are saving the value of your request to back.php in a variable, which won't be existing after the request is finished. You can either:
rename index.html to index.php, exchange your statement to
<?php echo $_POST['name']; ?>
and change your request to
$.get('/index.php', {name: "Bob"}, function(data) {
});
which would cause a continously reloading of the site,
or store your result in a $_SESSION (see PHP Session Handling), and retrieve that in your index.php (you got to rename the file).

How to assign a PHP variable value to JavaScript variable

In this code which I am posting i have one problem. I want my PHP variable to be stored in JavaScript variable but it shows error. The code is below.
<?php
$name="anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1=.$name.";"
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
Now when I am assigning $name to the variable name1 than I get an syntax error. Please let me know what changes I have to make. So that I get the value of the PHP variable $name stored in JavaScript variable name1.
In javascript with echo version: var name1= "'.$name.'";
<?php
$name = "anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1= "'.$name.'";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
And you can use like var name1= "<?php echo $name; ?>"; seperated html and php
<?php
$name="anurag singh";
?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1= "<?php echo $name; ?>";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
<?php
$name="anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1='.$name.'";"
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
echo it into a script tag
echo '<script> var name = '.$name.';</script>';
You can do it this way -
<?php $name="anurag singh"; ?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1="<?php echo $name ?>";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
You are pasing string $ame not variable as because you have used '...' this let the php know that its string no more variables inside this string.
<?php
$name="anurag singh";
?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1=<?pgp echo $name ?>;
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
try this
$(document).ready(function(){
var name1="'.$name.'";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
you can assign this value like var name= "'. $name.'";

BackboneJS with normal php server

I'm new to backboneJS so i have a problem sync the data from PHP server with backbone that i dont even get the json data from the php server to backbone model!Help me
Here is my php code
$data = array('1'=>array('id'=> 1, 'description'=>'Pick up milk', 'status'=> 'incomplete' ));
echo json_encode($data);
and the backbone js code
<html>
<head>
<script src="jquery-1.9.0.min.js" type="text/javascript"></script>
<script src="underscore-min.js" type="text/javascript"></script>
<script src="backbone.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
var TodoItem = Backbone.Model.extend({});
var todoItem = new TodoItem();
todoItem.url = '/backboneJS/todo.php';
console.log(todoItem.fetch());
</script>
<body>
</body>
</html>
I cant get the json data from the php to the backbone.What wrong with my code ?
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-2.0.3.min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.1/underscore-min.js" type="text/javascript"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js" type="text/javascript"></script>
</head>
<body>
</body>
<script type="text/javascript">
var TodoModel = Backbone.Model.extend({
urlRoot: '/backboneJS/todo.php',
initialize: function(){
// Place init message or else
}
});
var todoItem = new TodoModel();
todoItem.fetch({
success: function(todoResult){
console.log(todoResult);
console.log(todoItem.get(1));
}
});
</script>
</html>
Sticking to design pattern also, if you call your todoItem outside of nested function its likely to be executed before the actual fetch() is performed. Tested locally.

Categories