Ajax not passing by post method to php - php

Im using google chrome 65.0.3325.181 on windows 10, xampp is on to run the php. title explains the rest.
html/php:
$(document).ready(function (){
$('#sel_edificio').load('data.php');
$( ".form-control" ).change(function() {
var dato = 50;//document.getElementById("sel_edificio").value;
$.ajax({
method: "POST",
data: {'key': dato},
url: "uno.php",
success: function(status){
var asd = $('#test').load('uno.php');
//document.getElementById("NumEstudiantes").value(key);
}
});
});
});
</script>
uno.php:
<?php
echo $_POST['key'];
?>
error:
Notice: Undefined index: key in C:\xampp\htdocs\jqbd\uno.php on line 2

You are sending request two time, try this:
html/php
$(document).ready(function (){
$('#sel_edificio').load('data.php');
$( ".form-control" ).change(function() {
var dato = 50;//document.getElementById("sel_edificio").value;
$.ajax({
method: "POST",
data: {'key': dato},
url: "uno.php",
success: function(data){
$('#test').html(data);
}
});
});
});

change ajax method to type, try this
$.ajax({
type: "POST",
data: {'key': dato},
dataType: "json",
url: "uno.php",
success: function(status){
//var asd = $('#test').load('uno.php');
$('#test').load('uno.php', { key: dato });//document.getElementById("NumEstudiantes").value(key);
}
});

Related

How can I send a response in Ajax through php script

I need to send a response from the ajax to the php code. Alert msg as to be displayed when success.. I have to get entered date should be displayed in the url and response as to be sent..
<script>
$(document).ready(function() {
$('#txtdate').change(function(){
date = $(this).val();
$.ajax({
type: 'GET',
url: "http://localhost/data/check_date.php?date=" +date ,
success: function() {
alert(data);
}
});
});
});
</script>
Two error:
1:
date = $(this).val();
to
var date = $(this).val();
2:
url: "http://localhost/data/check_date.php?date=" +date ",
to
url: "http://localhost/data/check_date.php?date=" +date ,
3:
success: function() {
to
success: function(data) {
All script:
<script>
$(document).ready(function() {
$('#txtdate').change(function(){
var date = $(this).val();
$.ajax({
type: 'GET',
url: "http://localhost/data/check_date.php?date=" +date ,
success: function(data) {
alert(data);
}
});
});
});
</script>
Remove " mentioned at the end of the url because date is variable to be passed through url.
To overcome all mistakes change your whole code to
<script>
$(document).ready(function() {
$('#txtdate').change(function(){
var date = $(this).val();
$.ajax({
type: 'GET',
url: "http://localhost/data/check_date.php?date=" +date,
success: function(date) {
alert(date);
}
});
});
});
</script>
var date = $(this).val();
$.ajax({
type: 'GET',
url: "http://localhost/data/check_date.php?date=" +date ,
dataType: 'text',
success: function(data) { //add data in the function which is returned from the file
alert(data);
}
});
also try opening your network tab by clicking F12 in your browser before doing ajax request, if there's some error in your php file you can view it here in network tab, see if it helps

How to pass id with url to php file using ajax call?

I want to pass id with ajax to a php file. I tried a lot but couldnot succed. I want to return to the page with id. Please help me.
I tried like this.
<script>
jQuery.ajax({
var registration_date=$("#registration_date").val()
var building_length_ft=$("#building_length_ft").val();
var building_length_in=$("#building_length_in").val();
var building_breadth_ft=$("#building_breadth_ft").val();
var reg_id=$_GET['id'].val();
var dataString = $('#a').serialize();
$.ajax({
url:'registration_detail.php',
method:'POST',
data: dataString,
success:function(data){
alert(data);
}
});
});
</script>
I tried below code as well.
var reg_id=$_GET['id'].val();
$.ajax({
url:'registration_detail.php',
method:'POST',
data:{
'reg_id=' + reg_id,
},
success:function(data){
alert(data);
}
});
I tried below code as well.
var id = $('#id').val();
$.ajax({
url:'registration_detail.php',
method:'POST',
data: { id: id},
success: function(response) {
$('#result').html(response);
}
});
You should fix your code:
<script>
jQuery.ajax({
var registration_date=$("#registration_date").val()
var building_length_ft=$("#building_length_ft").val();
var building_length_in=$("#building_length_in").val();
var building_breadth_ft=$("#building_breadth_ft").val();
// There is a PHP script here:
var reg_id=<?= $_GET['id'] ?>;
var dataString = $('#a').serialize();
$.ajax({
url:'registration_detail.php',
method:'POST',
data: dataString,
success:function(data){
alert(data);
}
});
});
</script>

when I send data with ajax php wont get anything

post and get works fine but json returns wrong value , or something is wrong with my php code.
$(function () {
$('#username').on('keypress',function () {
var input = $('#username').val();
if(input.length>=4){
$.ajax({
url:'registration_php.php',
type: 'POST',
data:{username:input},
success:function () {
$.getJSON('registration_php.php',function (text) {
alert(text.user);
});
}
});
}
});
});
success:function(result) {
var items = JSON.parse(result);
alert(items['user']);
}
pass the result directly to your reponse as an argument like this
you should specify a dataType: "json" in your ajax call
var postData = JSON.stringify({
username: 'value'
});
var request = $.ajax({
url: "registration_php.php",
method: "POST",
dataType: "json",
data: postData,
});
request.success(function( results ) {
console.log(results)
});

ajax post with PHP

why isn't this working?
jQuery AJAX Code:
$("header input").bind("keyup", function()
{
var searchString= $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false
});
});
PHP Code(Just a test Code):
if($_POST["search"]) {
echo "TEST MESSAGE!";
}
It doesn't show The echo :/
thanks for ur help ;)
You need to display the data you receive from the ajax call.
Example, to put the result into a <div> called YourresultDiv:
Try with this
$("header input").on("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
success: function (data) {
$('#YourresultDiv').html(data);
alert("Successful");
}
});
});
Hopes this will help you....
$("header input").bind("keyup", function()
{
var searchString= $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
async: false
},success: function (data) {
$('div#posteddata').append(data);
}
);
});
<html>
<head>
</head>
<body>
<div id="posteddata"></div>
</body>
</html>
You need to specify an element you want to update.. for example
<div id="result"> </div>
and then append a success event handler to your ajax call
$("header input").bind("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false
}).success(function (data) {
$("#result").html(data);
}).fail(function () {
alert("Ajax failed!");
});
});
Try with this
In js add
success: function (data) {
// success handler
}
as your response handler
if($_POST["data"]) {
// search = search string available here in $_POST['data']
echo "TEST MESSAGE!";
}
where is your call back function in $.ajax() function,with callback function only,you can display anything through an ajax request..
So try this.
$("header input").on("keyup", function () {
var searchString = $("header input").val();
var dataString = 'search=' + searchString;
alert(dataString);
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
cache: false,
success: function (data) {
$('#Yourdiv').html(data); // or $('#Yourdiv')text(data);
}
});
});
Without success function,you can see the echoed statement in your network segment in console.
for that, press F12,then you will get a link like
XHR finished loading: POST "http://localhost/yourproject/func_name.Click on that link and you will goto network segment and from there,click on the function name and then in response or preview tab,you canb see the echoed statement..
try it.

html link value retrieve by Js ajax send to php

I have a html link with a value inside like this.
<a data-toggle='modal' data-id='1' href='#myModal' class='marker' title='Edit'>Link</a>
I have a Js script that trigger a php that I want to send the value data-id
<script>
$(document).on("click", ".marker", function () {
var myBookId = $(this).data('id');
$.ajax({
type: "post",
url: "update.php", //
data: myBookId,
success: function(msg) {
$("#thanks").html(msg)
},
error: function() {
alert("failure");
}
});
});
</script>
And in my php I have this
if (isset($_POST['myBookId'])) {
$emp_id = strip_tags($_POST['myBookId']);
echo $emp_id;
But something is wrong the value is not pass.
Your problem is on the params of the AJAX call. Try this way:
data: { myBookId: myBookId },
try
$('.marker')click(function(){
var myBookId = $().attr('data-id');
$.ajax({
type: "post",
url: "update.php", //
data: myBookId,
success: function(msg){
$("#thanks").html(msg)
},
error: function(){
alert("failure");
}
});
return false;
});

Categories