I ran two ajax one for post and one for get
This one is for posting form values by serialize.
$(function(){
function showValues() {
jQuery.ajax({
type: "POST",
data: $( "form" ).serialize(),
success: function(data){
var x = <?php echo json_encode($_SESSION['q10']); ?>;
console.log(x);
}
});
}
$( "input[type='checkbox'], input[type='radio']" ).on( "click", showValues );
$( "select" ).on( "change", showValues );
showValues();
});
and I store the values in variable
if(isset($_POST['q10']))
{
if($_POST['q10'] == 2){
$_SESSION['q10']=1;
}else{
$_SESSION['q10']=0;
}
}
then i use another ajax for catching the updated values but it gives me the old values
$(document).ready(function(){
$("#result").click(function(){
$.ajax({
type: 'GET',
success: function(res) {
var session = <?php echo(json_encode($page_session)); ?>;
if(session == 1){
document.getElementById("s1").style.display="block";
document.getElementById("s1").textContent = <?php echo(json_encode($_SESSION['q1'])); ?> ;
}
}
});
});
});
Related
I have this simple javascript which calls a PHP function flawlessly:
var showFeedPHP = "<?php showFeed($link, $category, $siteVersion, $cookie_index, $myuserlevel, $perPage, $showFrom); ?>";
$( "#sectionPosts" ).append( ""+showFeedPHP+"" );
I would like the last paramter, $showFrom, to be a javascript variable.
However, this version doesn't work and will just set the last parameter to the text string "+showFromJS+" :
var showFromJS = 10;
var showFeedPHP = "<?php showFeed($link, $category, $siteVersion, $cookie_index, $myuserlevel, $perPage, "+showFromJS+"); ?>";
$( "#sectionPosts" ).append( ""+showFeedPHP+"" );
I would like to use a javascript variable for declaring one of the php function parameters, but how?
You have to use Ajax in this case here is a sample
var showdeed= function(id) {
$.ajax({
url: 'path/to/php/file/showFeed',
type: 'GET',
data: {link:link,
category:category,...},
success: function(data) {
$( "#sectionPosts" ).append( ""+data+"" );
}
});
};
You must use AJAX to achieve your objective
function loadData() {
$.ajax({
url: "path_to_showfeed.php?var1="+<?php echo json_encode($link);?>+"&var2="+<?php echo json_encode($category);?>+"&var3="....
}).done(function(data) {
$( "#sectionPosts" ).append( ""+data+"" );
});
}
Also add this line to your head section
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
I got a simple POST call to a PHP file on my website. For some reason it's not working though. The console.log shows"undefined"
function check() {
var url = "../API/keychecker.php";
var spullen = $("keyval").val;
$.ajax({
type: "POST",
url: url,
data: {
key: spullen
},
success: function(data) {
console.log(data);
}
})
}
Here is the PHP file:
<?php
echo json_encode($_POST['key']);
?>
Your keyval call doesn't specify the type of element identifier. jQuery won't find the element you're looking for as you currently have it.
You must specify:
For classes:
$( ".keyval" ).val();
For ID
$( "#keyval" ).val();
For input name
$( "input[name=keyval]" ).val();
That should attach the value to the POST request.
var url = "API/keychecker.php";
var spullen = $( "keyval" ).val();
I am adding an autocomplete library into my project using the jquery-ui library.
I created the PHP script from which I need to get the data:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('connection.php');
$cid = $_SESSION['clinic_id'];
$searchTxt = '%'.$_POST['searchTxt'].'%';
$res = array();
$getPatients = "SELECT * FROM patient WHERE clinic_id = :cid and patient_name_en LIKE :searchTxt ORDER BY patient_id DESC";
$execGetPatients = $conn->prepare($getPatients);
$execGetPatients->bindValue(':cid', $cid);
$execGetPatients->bindValue(':searchTxt', $searchTxt);
$execGetPatients->execute();
$getPatientsResult = $execGetPatients->fetchAll();
$i = 0;
foreach($getPatientsResult as $result)
{
$res[$i] = $result;
$i++;
}
echo json_encode($res);
?>
And the JavaScript part is here:
<script>
$( function() {
$("#searchTxt").on('keyup', function(){
searchTxt = $("#searchTxt").val();
$.ajax({
url: '../php/autoComplete.php',
data: {searchTxt: searchTxt},
type: 'POST',
dataType: 'JSON',
success:function(resp)
{
$.each( resp, function(key, result)
{
var availableTags = result['patient_name_en'];
});
},
error:function(resp)
{
console.log(resp)
}
})
} );
$( "#searchTxt" ).autocomplete({
source: availableTags
});
});
</script>
I had the following error in the console about jQuery:
Maximum call stack size exceeded.
But now it gone somehow, and I don't know why.
Now after typing in the search text box, I am getting an empty array at the network tab of the developer tool or an array but with no properties and nothing is show as autocomplete near the text box:
EDIT
I changed a line in PHP into:
$searchTxt = '%'.$_POST['searchTxt'].'%';
And now no php errors, but a JavaScript error:
Uncaught ReferenceError: availableTags is not defined
$( "#searchTxt" ).autocomplete({
source: availableTags
});
success:function(resp)
{
$.each( resp, function(key, result)
{
var availableTags = result['patient_name_en'];
});
},
You have declared the availableTags inside the success method of ajax call and you are trying to access it outside it's scope.
Either you make availableTags as global variable or declare somewhere on the top so that you can access it in both the places ( for reassigning after ajax success and in the autocomplete method).
$( "#searchTxt" ).autocomplete({
source: availableTags
});
});
this code gets executed before your post gets success, it's non-blocking, so you have to write it something like.
$( function() {
$("#searchTxt").on('keyup', function(){
searchTxt = $("#searchTxt").val();
$.ajax({
url: '../php/autoComplete.php',
data: {searchTxt: searchTxt},
type: 'POST',
dataType: 'JSON',
success:function(resp)
{
$.each( resp, function(key, result)
{
var availableTags = result['patient_name_en'];
$( "#searchTxt" ).autocomplete({
source: availableTags
});
});
});
},
error:function(resp)
{
console.log(resp)
}
})
} );
});
First, thanks for you reading. Here is my code
scripts/complete_backorder.php
<?php
if(!isset($_GET['order_id'])) {
exit();
} else {
$db = new PDO("CONNECTION INFO");
$order = $db->prepare("UPDATE `scs_order` SET `order_complete`= 1 WHERE `order_id` = :var");
$order->bindValue( ':var',$_GET['order_id'] );
if ( $order->execute() ) {
echo "DONE";
};
};
?>
js/tabs.js
/*
[#]===============================================================================[#]
MODAL: "complete_backorder_Modal"
USAGE: modal to confirm whether or not user want to complete a backorder.
[#]===============================================================================[#]
*/
$(function(){
$(" .remove_record ").click(function( event ){
event.preventDefault();
var rows = $(this).parent().parent().parent().parent().find("tr:last").index() + 1;
var order = $(this).attr("href");
var dataString = 'order_id='+order;
$( '#complete_backorder_Modal' ).modal({
keyboard: false,
backdrop: 'static'
});
$( '#complete_backorder_Modal #modal-yes' ).click(function(e){
$.ajax({
type: "POST",
url: "../scripts/complete_backorder.php",
data: dataString,
success: function(data){
alert("Settings has been updated successfully.");
}
});
});
});
});
so i know the php code is working as ive tested it over and over manually. but when i click on the ".remove_record" button the modal shows and i click the yes button in the modal the alert boxes shows up to say it was successful but when i look at the database nothing has changed.
any ideas?
Your SQL is never running becuase there are no $_GET variables, your are using $_POST But even if you did you are not passing through order_id correctly. Change:
var postData = {'order_id ' : order}; // instead of var dataString = 'order_id='+order;
And
$.ajax({
type: "POST",
url: "../scripts/complete_backorder.php",
data: postData, // instead of dataString
success: function(data){
alert("Settings has been updated successfully.");
}
});
In the PHP change:
if(!isset($_POST['order_id'])) { // Insetad of $_GET
And
$order->bindValue( ':var',$_POST['order_id'] );
I'm trying to pass some variables to a php file with ajax
$(document).ready(function(){
$("button").click(function(){
var id = this.id;
var name=this.name;
console.log(id+" "+name);
$.ajax({
type: 'GET',
url: 'utility.php',
dataType: 'text',
data: {id: id, name: name},
success: console.log('aa'),
//error: function( jqXhr, textStatus, errorThrown ){ console.log( errorThrown ); },
//complete: alert(id+' '+name)
}); // Ajax Call
}); //event handler
}); //document.ready
<?php
warning('getting something');
if($_GET['id'] && $_GET['name']){
$id=$_GET['id'];
$name=$_GET['name'];
if($id=='delete'){
my_remove($name);
}
if($id=='modify'){
retrieve($name);
my_remove($name);
modify($name);
}
}
else {
warning('unable to get information');
}
function my_remove($name){
warning('deleting');
//mysqli_query($con,"DELETE FROM `book`.`".$page."` WHERE `".$page."`.`name` =\'".$name."\'");
//echo "<script type='text/javascript'>alert('$name');</script>";
}
function modify($name){
warning('modified');
}
function retrieve($name){
warning('fetching');
}
function warning($message){
echo "<script type='text/javascript'>alert('$message');</script>";
}
?>
The .js part seems to run smoothly, it sets the name and id as it should and returns a success message, but nothing else happens, not even the alert('getting something') which should run regardless of parameters.
Printing out the data gives [object Object] which I'm not even sure what it means.
Please help!
you're using GET not POST so under the line
if($_GET['id'] && $_GET['name']){
should be get, not post as you have it
$id=$_GET['id'];
$name=$_GET['name'];
since your php script runs in the background with your ajax call, I don't think the alert code in that page will work. Instead try returning the plain text to the ajax function and alert it there
$(document).ready(function(){
$("button").click(function(){
var id = this.id;
var name=this.name;
console.log(id+" "+name);
$.ajax({
type: 'GET',
url: 'utility.php',
dataType: 'text',
data: {id: id, name: name},
}).done(function(text){
alert(text);
}); // Ajax Call
}); //event handler
}); //document.ready
and your php file like this.I changed your warning function
<?php
warning('getting something');
if($_GET['id'] && $_GET['name']){
$id=$_GET['id'];
$name=$_GET['name'];
if($id=='delete'){
my_remove($name);
}
if($id=='modify'){
retrieve($name);
my_remove($name);
modify($name);
}
}
else {
warning('unable to get information');
}
function my_remove($name){
warning('deleting');
//mysqli_query($con,"DELETE FROM `book`.`".$page."` WHERE `".$page."`.`name` =\'".$name."\'");
//echo "<script type='text/javascript'>alert('$name');</script>";
}
function modify($name){
warning('modified');
}
function retrieve($name){
warning('fetching');
}
function warning($message){
echo $message;
}
?>