Problems with jQuery/AJAX interaction - php

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;
});
});

Related

Jquery: Not Receiving Data from php page through Ajax

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<span id="response">
</span>
<script>
$(document).ready(function () {
$("#open-comm").click(function () {
$.ajax({
url: "http://en.wikilistia.com/fetch_comments.php",
type: "POST",
SUCCESS: function (responsedata) {
$("#response").html(responsedata);
alert("sucess");
}
});
});
});
</script>
</body>
</html>
Not Receiving responsedata from php page. In the browser php file showing data but through ajax not showing anything.
What's the problem with PHP or Jquery code. I am trying first time this.
Here you go with a solution
$(document).ready(function () {
$("#open-comm").click(function () {
$.ajax({
url: "http://en.wikilistia.com/fetch_comments.php",
type: "POST",
success: function (responsedata) {
$("#response").html(responsedata);
alert("sucess");
},
error: function(error) {
console.log(error);
}
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<head>
<title>Page Title</title>
</head>
<body>
<span id="response"></span>
</body>
It's a typo SUCCESS should be success

AJAX send value from js to php

been tring to send var form js to php and do some manipulation but I keep receving this error. Is it even possible to send the value through to php?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<script
src="https://code.jquery.com/jquery-3.5.0.min.js"
integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ="
crossorigin="anonymous">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/d3js/5.15.1/d3.min.js"></script>
</head>
<body>
<input type="text" name='name'>
<p id="hey"></p>
<?php
echo $_POST['name'];
?>
<button id='button'>hi</button>
</body>
<script src="index.js"></script>
</html>
$("#button").click(function(){
$.post("index.php",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data, status){
$("#hey").append(status);
});
});
I created one index.php file here this should work for you.
<?php
if ($_POST) {
echo json_encode(['data' => $_POST['name'], 'success' => true]);
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<script
src="https://code.jquery.com/jquery-3.5.0.min.js"
integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ="
crossorigin="anonymous">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/d3js/5.15.1/d3.min.js"></script>
</head>
<body>
<input type="text" name='name'>
<p id="hey"></p>
<div id='test'></div>
<button id='button'>hi</button>
</body>
<script>
let data = {
name: "Donald Duck",
city: "Duckburg"
};
$("#button").click(function(){
$.ajax({
type: "POST",
url: 'index.php',
data: data,
dataType: 'json'
}).done(function(response) {
if (response.success) {
$('#hey').html('success');
$('#test').html(response.data);
}
});
});
</script>
</html>
This does not work because when you first load the page $_POST['name'] is undefined. Your javascript code does not trigger a submit (= reload to send the data). $_POST['name'] is never set because it will not be updated on the fly. To make this work make a separate php file which only handles the POST data from your javascript and returning a value.
/* call post.php and add console.log to track data */
$("#button").click(function(){
$.post("post.php",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data, status){
$("#hey").append(status);
console.log(data);
});
});
<?php
// post.php
if($_SERVER['REQUEST_METHOD'] == "POST") {
echo $_POST['name'];
}
This is of course very basic. In order to communicate effectively you should look into formating the data e.g. into JSON.

get and show constant from external webpage and update when change

this script receive time from www.time.is and from #twd ID and show when click on the send btn.
I have two problems:
1-only show first receive time and don't update when click on send again
2- page refresh and lose data
I have Three problem with below code:
<?php include 'simple_html_dom.php'; ?>
<!DOCTYPE html>
<html>
<head>
<title>Untitled 1</title>
<script src="js/jquery.js"></script>
</head>
<body>
<form action="" method="POST">
<input id="url" name="url" type="text" value="http://www.time.is/">
<input id="address" name="address" type="text" value="#twd">
<input id="send" type="submit" value="get">
</form>
<ul id="times">
</ul>
<script type="text/javascript">
$("#send").click(function(events) {
events.preventDefault();
var url = $("#url").val();
var address = $("#address").val();
var dataString = 'url=' + url + '&address=' + address;
$.ajax({
type: "POST",
url: "read.php",
data: dataString,
success: function() {
var result = "<?php echo getme($url,$address); ?>";
alert(result);
$('#times').append('<li></li>');
}
});
return true;
});
</script>
<?php
function getme($url, $address) {
$html = file_get_html($url);
$code = $html->find($address, 0);
return $code;
}
echo getme($url, $address);
?>
</body>
</html>
any body can help me ..
Thanx all
Try this:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
function timer(){
$.post( "process.php", function( data ) {
$('#times').append('<li>'+data+'</li>');
});
}
setInterval(function(){timer()},1000);
});
</script>
</head>
<body>
<ul id="times"></ul>
</body>
</html>
And put this in your process.php file:
<?php
include 'simple_html_dom.php';
$html = file_get_html('http://www.time.is/');
foreach($html->find('#clock0') as $element){
echo $element->plaintext;
}
?>
This is my test result:
11:15:43AM
11:15:46AM
11:15:51AM
11:15:53AM
11:15:53AM
11:16:10AM
11:15:52AM
11:15:42AM
11:16:09AM
11:16:17AM
11:16:12AM
Note:
1- It is advisable that you change intervals from 1000 milliseconds (1 second).
2- Don't forget to use clearInterval() after specific repeat.

Cant get reply from ajax

I am working on trying to post to ajax file with jquery and showing result on page posted from. Post gets posted but I get no reply from ajax. Am i missing something here?
form file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Ajax</title>
<script src="js/jquery-1.10.2.js"></script>
</head>
<body>
<form id="myform" />
<input type="text" id="youTyped" value=""/>
<input type="submit" value="Submit">
</form>
<div id="answer"></div>
<script type="text/javascript">
$('#myform').submit(function(){
var youTyped = $("#youTyped").val();
var data = {youTyped:youTyped};
$.ajax({
type: "POST",
url: "scripts/ajaxTest.php",
data: data,
success: function(response){
$("#answer").html(response);
}
});
});
</script>
</body>
</html>
ajax file
<?php
if(isset($_POST['youTyped'])){
$youTyped = $_POST['youTyped'];
}
echo $youTyped;
?>
Well, first of all, you didn't write your form correctly, the elements you want to send are not inside the form.
Second, your js function is not avoiding the form for being sent, so you're reloading the page when submitting the form.
So, to correct it, and get the answer, just change the code to:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Ajax</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
</head>
<body>
<form id="myform">
<input type="text" id="youTyped" value=""/>
<input type="submit" value="Submit">
<div id="answer"></div>
</form>
<script type="text/javascript">
$('#myform').submit(function(e){
// Avoid send the form as a normal request
e.preventDefault();
var youTyped = $("#youTyped").val();
var data = {youTyped:youTyped};
$.ajax({
type: "POST",
url: "scripts/ajaxTest.php",
data: data,
success: function(response){
$("#answer").html(response);
}
});
});
</script>
</body>
</html>
One last thing. To test it, when you're sending and receiving AJAX, if you're using Chrome you can use the development tools to know if you're sending correctly the requests. Just press F12, and then go to Network tab. If you're using correctly AJAX, you'll see how new lines are added when you press the submit button:
You need to return false on form submit. The form is being submited before ajax request. Also add a closing tag for form.
<script type="text/javascript">
$('#myform').submit(function () {
var youTyped = $("#youTyped").val();
var data = {
youTyped: youTyped
};
$.ajax({
type: "POST",
url: "scripts/ajaxTest.php",
data: data,
success: function (response) {
$("#answer").html(response);
}
});
return false;
});
</script>
Try if this code is working:
<body>
<input type="text" id="youTyped" value=""/>
<input type="submit" id="submit" value="Submit">
<div id="answer"></div>
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(function(){
var youTyped = $("#youTyped").val();
$.post('scripts/ajaxTest.php',{"youTyped" : youTyped},function(response)
{
$("#answer").html(response);
});
});
});
</script>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Ajax</title>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
$('#myform').submit(function(event){
event.preventDefault();
var youTyped = $("#youTyped").val();
var data = {youTyped:youTyped};
$.ajax({
type: "POST",
url: "ajaxTest.php",
data: data,
success: successMsg,
error: errorMsg
});
});
function successMsg(response){
alert(response);
$("#answer").html(response);
}
function errorMsg(){
alert("Invalid Ajax Call");
}
</script>
</head>
<body>
<form id="myform" />
<input type="text" id="youTyped" value=""/>
<input type="submit" value="Submit">
<div id="answer"></div>
</body>
</html>
As I toyed with the idea, thought that this would make a good addition/contribution to the question.
I added and modified a few things.
One of which being:
If nothing was typed into the form input field, it would return "You typed: Nothing".
PHP:
<?php
if(!empty($_POST['youTyped'])){
$youTyped = $_POST['youTyped'];
echo "You typed: " .$youTyped;
}
else if (empty($_POST['youTyped'])){
echo "You typed: Nothing";
}
?>

Ajax pass variable to PHP with GET

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

Categories