Ajax pass variable to PHP with GET - php

The variable $pos has no value after that the Submit button I pressed.
<?php
$pos = $_GET['pos'];
echo "<br><br>pos = ".$pos;
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=us-ascii">
<title></title>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'></script>
<script type='text/javascript'>
$(window).load(function(){
var position = 128;
$('#submit').click(function(){
$.ajax({
type:"GET",
url: "ajax_test.php",
data: { pos : position },
success: function(){
//do stuff after the AJAX calls successfully completes
}
});
});
});
</script>
</head>
<body>
<br>
<button id="submit">Submit</button>
</body>
</html>
The web console in FF shows:
[12:41:55.027] GET http://somehost.com/ajax_test.php?pos=128 [HTTP/1.1 200 OK 187ms]

<?php
if($_SERVER['REQUEST_METHOD']=='GET' && isset($_GET['ajax'])){
$pos = $_GET['pos'];
echo $pos; exit;
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=us-ascii">
<title></title>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js'></script>
<script type='text/javascript'>
$(window).load(function(){
var position = 128;
$('#submit').click(function(){
$.ajax({
type:"GET",
url: "test.php",
data: { pos : position , ajax:1},
success: function(response){
document.getElementById('response').innerHTML ='pos='+response
//do stuff after the AJAX calls successfully completes
}
});
});
});
</script>
</head>
<body>
<br>
<div id="response">pos=</div>
<button id="submit">Submit</button>
</body>
</html>

Your code is correct, your just not using the result of your Ajax GET Request.
Try this in your success function:
success: function(data) {
alert(data);
}

I don't see any errors in your code, except that you call your PHP script from your JavaScript code, and don't do anything with the output. The echo $pos output isn't used anywhere. Add this to your script:
success: function(result){
$('#YourResultDiv').html(result);
}

You can't retrieve your variable from the AJAX call.
Ajax is async and you can't modify a php code which is already computed.
But you can insert your data with jQuery with appendTo() for example

Related

Very simple AJAX-Call does not work

This very simple AJAX-Call does not work on my localhost. I do have a Windows 10 Machine with XAMPP running. I tracked the packages, and the AJAX-Reqauest is not even sent to handle.php. What am i doing wrong here?
ajaxTest.php
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js">
$(document).ready(function()
{
$.ajax(
{
type: 'post',
url: 'inc/handle.php',
success: function(data)
{
alert("Done!");
}
});
});
</script>
</head>
</html>
handle.php
<?php
echo "Test!";
?>
The problem is: include jquery on script tag and your code into another script tag
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function()
{
$.ajax(
{
type: 'post',
url: 'inc/handle.php',
success: function(data)
{
alert("Done!");
}
});
});
</script>
</head>
</html>

Problems with jQuery/AJAX interaction

I've been studyiing jQuery lately and today I was working with AJAX. What I want to do, right now, is simply send a date from a date picker to a PHP page which sends back that date.
I'll later improve the PHP page to do what I need to do.
Here's the code of the HTML page:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="AlterVista - Editor HTML"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
$(document).ready(function() {
$("#foo").submit(function(){
var serializedData = $(this).serialize();
$.post('demo.php', serializedData, function(response) {
// Log the response to the console
console.log("Response: "+response);
$("#result").text() = response;
});
});
});
</script>
<title></title>
</head>
<body>
<form id='foo'>
<input type='date' id='dataI' name='dataI'>
<input type='submit' value='send'>
<p id='result'></p>
</form>
</body>
</html>
And here's the simple php page I made:
<?php
echo $_POST["dataS"];
?>
But when I try to send data nothing happens. What am I doing wrong? I tried using a solution I saw here on stack overflow but it doesn't work.
I am attaching code sample;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="AlterVista - Editor HTML"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
function validate(){
var serializedData = $('#foo').serialize();
$.post('ajax.php', serializedData, function(response) {
// Log the response to the console
console.log("Response: "+response);
$("#result").html(response);
});
return false;
}
</script>
<title></title>
</head>
<body>
<form id='foo' action="#" onsubmit="return validate()">
<input type='date' id='dataI' name='dataI'>
<input type='submit' value='send'>
<p id='result'></p>
</form>
</body>
</html>
Ajax code file;
echo $_POST["dataI"];
Try adding return false; after
$.post('demo.php', serializedData, function(response) {
// Log the response to the console
console.log("Response: "+response);
$("#result").text() = response;
});
statement.
$(document).ready(function() {
$("#foo").submit(function(){
var serializedData = $(this).serialize();
$.post('demo.php', serializedData, function(response) {
// Log the response to the console
console.log("Response: "+response);
$("#result").text() = response;
});
return false;
});
});

Simple php ajax html issue

simple question for most of you but for me, being a newbie in php and jquery+ajax, not really: how to replace my index.html with some other html code, requested by ajax call from a php file?
index.html
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Login page</h1>
<button id="btn_login">Login</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="javascript.js"></script>
</body>
</html>
javascript.js
$('#btn_login').click(
function(){
$.ajax({
url: "login.php",
type: "GET",
success: function(data){
// what to do here?
}
});
}
)
login.php
<?php
echo '<div>Succesful login</div>';
?>
TLDR: I want to replace the "Login page" + login button screen to "Succesful login" when clicked the button.
Thank you
you need to identify -or classify- your <h1> tag to be easily able to change
it's contents ,
<h1 id='title'>Login page</h1>
then , within your ajax call, if success you can change the content like that:
success: function(data){
// what to do here?
if (data) {
$('#title').html(data);
$('#btn_login').remove();
}
}
Take a look: ( https://www.w3schools.com/xml/tryit.asp?filename=tryajax_first : w3schools )
...
You can try this:
$('#btn_login').click(
function(){
$.ajax({
url: "login.php",
type: "GET",
success: function(data){
document.body.innerHTML = ""; // remove all content from the document
document.write(data); // write the returned div to the document
}
});
}
)

.ajax() not sending data to php file

I am having a problem is retrieving data sent to a php file through .ajax() via jquery
Following is my html:
<!-- Jquery tute no 94 onwards -->
<html lang="en">
<head>
<meta charse="utf-8">
<title> jquery4 </title>
<link rel="stylesheet" type="text/css" href="jquery4.css"/>
</head>
<body>
<input id="lo" type="text"> </input>
<input id="ton" type="button" value="Load"> </input>
<div id="content"> </div>
<script type="text/javascript" src="jquery.js"> </script>
<script type="text/javascript" src="jquery4.js"> </script>
</body>
</html>
My jquery4.js is:
$(document).ready(function()
{
$('#ton').click(function()
{
var nm= $('#lo').val();
$.ajax({url: 'page.php', data1: 'name='+nm, success: function(data2)
{
$('#content').html(data2);
}
});
});
});
My page.php is:
<?php
if(isset($_GET['data1']))
{
echo $namer= $_GET['data1'];
}
?>
All the above files are in the same folder, and I have xampp installed.
I guess the error is somewhere in the jquery file where I call the
ajax() function
jQuery ajax doesn't take a data1 parameter. It takes a data parameter, which should be an object of name-value pairs.
$.ajax({
url: 'page.php',
data: {
data1: 'name=' + nm,
},
success: function(data2) {
$('#content').html(data2);
}
});
$.ajax({
type: "GET",
url: "page.php",
data: {
data1: 'name=' + nm,
}
,
success: function(data2) {
$('#content').html(data2);
}
});
Try this:
$(document).ready(function() {
$('#ton').click(function() {
var nm= $('#lo').val();
$.ajax({
url: 'page.php?name=' +nm,
success: function(data2) {
$('#content').html(data2);
}
});
});
});
You don't have to tell jQuery to use GET, as it defaults to that, if nothing else is specified.
So the ajax function does not take an argument called data1, but 'data', this is mostly used for other methods as POST, PUT and DELETE.
I prefer also sending GET requests with a normal query string, like the above example.
You can then check for get GET parameter with PHP, using $_GET['name']

Pass javascript variable to php with ajax and the result doesn't show anything

This is my code and i want to pass javascript variable with ajax to php when i click submit button then the result doesn't show var_data variable from javascript What code is wrong?
This is edit order one before everybody help me
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#sub').click(function() {
var var_data = "Hello World";
$.ajax({
url: 'http://localhost/ajax/PassVariable.php',
type: 'GET',
data: { var_PHP_data: var_data },
success: function(data) {
// do something;
}
});
});
});
</script>
</head>
<body>
<input type="submit" value="Submit" id="sub"/>
<?php
$test = $_GET['var_PHP_data'];
echo $test;
?>
</body>
</html>
and this is source code now
<?php
if (isset($_GET['var_PHP_data'])) {
echo $_GET['var_PHP_data'];
} else {
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
$(document).ready(function() {
$('#sub').click(function() {
var var_data = "Hello World";
$.ajax({
url: 'http://localhost/test.php',
type: 'GET',
data: { var_PHP_data: var_data },
success: function(data) {
// do something;
$('#result').html(data)
}
});
});
});
</script>
</head>
<body>
<input type="submit" value="Submit" id="sub"/>
<div id="result">
</body>
</html>
<?php } ?>
this statement if(isset($_GET['var_PHP_data'])) output false and then show Hello World What should i do to do for isset($_GET['var_PHP_data']) is true?
Your solution has PHP issues: you don't check if the data exists, and also, you don't do anything with the result. I've modified the script to do the following:
Check if the var_PHP_data var is set (in PHP, on the server).
If yes, just send a blank text response containing that data.
If no, then draw the form and everything else.
In the form, I've created a #result div.
Ajax response will be shown in this div.
Also make sure that you host the script at localhost and that it is called test.php. To make sure this is resilient, you can change the Ajax URL to
<?php echo $_SERVER['PHP_SELF'];?> to make sure that you'll hit the correct script.
<?php
if (isset($_GET['var_PHP_data'])) {
echo $_GET['var_PHP_data'];
} else {
?>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js">
<script>
$(document).ready(function() {
$('#sub').click(function() {
var var_data = "Hello World";
$.ajax({
url: 'http://localhost/test.php',
type: 'GET',
data: { var_PHP_data: var_data },
success: function(data) {
// do something;
$('#result').html(data)
}
});
});
});
</script>
</head>
<body>
<input type="submit" value="Submit" id="sub"/>
<div id="result">
</body>
</html>
<?php } ?>
Try jQuery Form its this will help to solve many problems.
For you question: try url without domain name, add tags 'form', change event click to submit, add data type
what are the contents of PassVariable.php ? if is the same where you have they jquery bit wont work coz php will print all the page again, if the file is different try
success: function(data) {
alert('databack = '+ data);
}
Try placing your input into a form and attaching the ajax call to the form onsubmit event. The way it happens in the provided happen is when you click in the field, in which case it submits before you can write anything really.
$(document).ready(function() {
$('#brn').click(function() {
var var_data = "Hello World";
alert("click works");
$.ajax({
url: 'http://localhost/ajax/PassVariable.php',
type: 'GET',
data: { x: var_data },
success: function(data) {
alert(data);
}
});
});
});
change it to this code
then in PassVariable.php put
make button
<input type="button" id="btn" value="click me" />
it should work because it is very basic example. If it doesn't work check your console if there are any JavaScript errors and remove them.

Categories