hi every body i was testing a code for checking name availability with ajax and i dont now why its doesnt work
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.js"></script>
<link href="style.css" rel="stylesheet" type="text/css" />
<title>index</title>
</head>
this is the body and the jquery function
check
<div id="answer" >
<script type="text/javascript">
//script
$(document).ready(function() {
$("#checkme").click(function(){
$("#answer").ajaxStart(function() {
$(this).text("loading .. .").show("slow");
});
var Userme = $("#username").val();
$.ajax({
type:GET,
url:"file.php",
data:"username=" + Userme,
success: function(msg){ $("#answer").text(msg).fadeIn("slow");}
});
});
});
</script>
</div></form>
</body>
</html>
and this is php code
<?php
$names = array (A,B,C,D);
$username = $_GET['username'];
if(in_array ($username , $names)){
echo "ok";
}else{
echo "doesnt exist ";
}
?>
plz help
im new with jquery and ajax and i cant guess the mistake i hope you can help me
It's unclear what exactly the problem is as I can see multiple errors.
In your javascript code:
type:GET should be type: "get"
data:"username=" + Userme should be data: {username: Userme}
In your PHP code:
$names = array (A,B,C,D); should be $names = array ("A", "B", "C", "D");
Related
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.
Lately I've been experimenting with AJAX and jQuery. But somehow $.post method doesn't seem to work. Anybody got solutions?
Here's my code.
<html>
<meta charset="utf-8">
<head>
<script type="text/javascript" src="jquery-3.3.1.js"></script>
<script type="text/javascript">
function send(){
$.post('t.php', {stuff:1}, function(data){
if(data == 'success'){
alert('works');
}
});
}
</script>
</head>
<body>
<div id="btn" onclick="send()">CLICK</div>
</body>
</html>
and my t.php:
<?php echo "success";?>
It works actually but you don't know that how to get response from php file properly
Change ajax code like below:
$.post('t.php', {stuff:1}, function(data){
if(data[0] == 's'){//changed here. data is an array not string
alert('works');
}
});
And in php
<?php echo "s";?>
I am trying to learn Ajax/JavaScript and I can not seem to get my search to work. It is meant to return partial names but returns nothing at all
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js></script>
<script type="text/javascript">
function getName(value) {
$.post("searchbar.php", {partialName:value},function(data))
$("#results").html(data);
}
</script>
</head>
<body>
<input type="text" onkeyup="getName(this.value)"/>
<br>
<div id="results"></div>
</body>
</html>
php file:
<?php
include "header.php";
$partialName = $_POST['partialName'];
$name = mysql_query("SELECT username FROM grpgusers WHERE username LIKE '%$partialName%'");
while($names = mysql_fetch_array($name)){
echo "<div>".$names['username']."</div>";
}
?>
could some one please help me out on where I am going wrong?
you are missing braces, change to:
function getName(value) {
$.post("searchbar.php", {partialName:value},function(data) {
$("#results").html(data);
});
}
Plus, missing a quote in
jquery.min.js ></script>
^ right there
change that to jquery.min.js"></script>
Missing closing double quote in <script> tag and closing brace for $.post() function :
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
function getName(value) {
$.post("searchbar.php", {partialName: value}, function (data) {
$("#results").html(data);
});
}
</script>
</head>
<body>
<input type="text" onkeyup="getName(this.value)"/>
<br>
<div id="results"></div>
</body>
</html>
Turns out I did not double quote the link to the js libary
thanks
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
POST PHP to itself using AJAX does not return the expected value?
Parameters has been successfully send to POST, based on Firebugs Console
but it does not dispaly the result to the same page.
How to fix this?
labor.php
<?php
if(isset($_POST['from']) && isset($_POST['from']))
{
echo 'from: '.$_POST['from'].
' to: '.$_POST['to'];
//do something here
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Labor Reports</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker_from" ).datepicker();
$( "#datepicker_to" ).datepicker();
$( "button" )
.button()
.click(function() {
$.ajax({
url: 'labor.php',
type: 'POST',
data: { from: $('#datepicker_from').val().trim(), to: $('#datepicker_to').val().trim() },
success: function(){
}
});
});
});
You aren't doing anything with the returned data.
success: function(data){
alert(data);
}
data represents the response sent back from the server. In your case, it will be the echoed output of your from and to $_POST values. Coincidentally, you will also get the rest of the page sent back to you as well, you'll probably want to return false; after the echoes, so that it stops the printing of the page.