Call PHP function with javascript with a javascript parameter - javascript

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>

Related

Simple Jquery POST call not working

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

couldn't catch the updated value in ajax

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'])); ?> ;
}
}
});
});
});

How to get another page on ajax?

I've a problem with my page, first code (let said is hompe.php)
<html>
<div id="trackingkp"></div>
</html>
then on my ajax I've code like these
$( document ).ready(function() {
var dataString = '';
$.ajax
({
type: "POST",
url: host+"ajax/tracking/kp",
dataType: 'html',
success: function(html)
{
$('#trackingkp').html($(html).find('#trackingkp').html());
//$("#trackingkp").html($(data).find('#trackingkp').html());
}
});
});
and on my ajax controller, I do like these (I'm using framework laravel)
public function tracking($url)
{
//var_dump("AAA");
$this->view('ajax/tracking/'.$url,[]);
}
on my ajax/tracking view like this
<?php
//$json = array();
$json = "<input type='text'></input>";
echo json_encode($json);
?>
When I try that is showing
Syntax error, unrecognized expression: "<input type='text'>
I have solved the problem, I'm using another option without using jQuery, so on same time, on controller when load view home.php, I try put load view too like these
public function index(){
is_header();
$this->view('home',[]);
$this->view('ajax/tracking/kp',[]); ---> I add these
is_footer();
}
and on ajax/tracking/kp (view) I put these code
<input type='text'></input>
and is working.
The problem would be in this one:
success: function(html) {
// Problem : $(html).find('#trackingkp').html();
$('#trackingkp').html($(html).find('#trackingkp').html());
}
From which this html response is expected to return a json value
in your given [controller]
<?php
//$json = array();
$json = "<input type='text'></input>";
echo json_encode($json);
?>
Thus this is equivalent to:
$("<input type='text'></input>").find('#trackingkp').html();
In this case, I don't think you might need to call another *.html(). You can simplify / solve this by calling it once:
$.ajax({
type: "POST",
url: host+"ajax/tracking/kp",
dataType: 'json', // Kindly replace [html] to [json] since you are returning a [json] value
success: function(html) {
$('#trackingkp').html(html);
}
});
Hope this helps for your case

live search bootstrap with ajax and query

Me and some mates have been trying to make a live search script on our search bar. Now this isn't going that well so now we are asking for ur help!
our external file to get the results is this :
$con = mysqli_connect('localhost', '*', '*', '*');
$key=$_POST['search'];
$query = ("select name, url from search where name LIKE '%{$key}%'");
$sql = $con->query($query);
while($row = $sql->fetch_array()){
echo json_encode($row);
}
and our script code looks like this :
<script>
$(document).ready(function(){
$( "#formGroupInputLarge" ).keyup(function() {
console.log( "Handler for .keyup() called." )
var string = $('#formGroupInputLarge').val();
$.ajax(
{
type: 'POST',
url: 'search.php',
data: {'search': string},
success: function(data){
var text= JSON.parse(data);
$("#suggesstion-box").show();
$("#suggesstion-box").html("<a href='#'>"+ text +"</a>");
$("#search-box").css("background","#FFF");
}
}
);
});
});
</script>
we've tried with multiple things like the next ones :
<script>
$(document).ready(function () {
$("#formGroupInputLarge").keyup(function () {
console.log("Handler for .keyup() called.");
var string = $('#formGroupInputLarge').val();
$.ajax(
{
type: 'POST',
url: 'search.php',
data: {'search': string},
success: function (data) {
var obj = eval('('+ data +')' );
console.log(obj['name']);
//var text = JSON.parse(data);
//$("#suggesstion-box").show();
//$("#suggesstion-box").html(text);
//$("#search-box").css("background", "#FFF");
}
}
);
});
});
</script>
but none of it seems to work. Please help us!
So many things could be wrong in your code !
You need to give more info on how each part behaves (error messages, etc).
Check this to get a good look at how everything work with AJAX, and nice and simple examples http://www.w3schools.com/php/php_ajax_intro.asp

Ajax function not working when passing one of the variables through parameter, PHP, JAVASCRIPT, AJAX

I'm trying to write a function to get the value from a select statement, and echo the value to the screen. It works when I put the exact select name inside of the ajax function, but it stopped working when I tried to pass the select name through the parameters. I know it was passed through the parameter successfully, and I know it was the right select name by alerting it to the screen inside the function when I was testing it. So I think the actual problem might be on this line:
data: { select: $('select[name=theName]').val()},
But I'm not sure what is wrong with it. I have 2 versions of my codes below. The first version works, and the second version doesn't. The first version has the exact select name inside the parameter, and the second version is passed through the parameter called 'theName'. Please take a look:
ajax.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function ajax(url,theName,id) {
alert(theName)
$.ajax({
type: "POST",
url: url,
data: { select: $('select[name="select1"]').val()},
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById( id ).innerHTML = data;
}
});
}
</script>
test1
<?php
echo "<select name = 'select1' onchange = 'ajax(\"test2.php\",\"select1\",\"output\")'>";
?>
^---This version works.
ajax.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function ajax(url,theName,id) {
alert(theName)
$.ajax({
type: "POST",
url: url,
data: { select: $('select[name=theName]').val()},
error: function(xhr,status,error){alert(error);},
success:function(data) {
document.getElementById( id ).innerHTML = data;
}
});
}
</script>
test1
<?php
echo "<select name = 'select1' onchange = 'ajax(\"test2.php\",\"select1\",\"output\")'>";
?>
^---This version does not work.
You have omitted double quotes and concatenation here:
data: { select: $('select[name="'+theName+'"]').val()},
data: { select: $('select[name=' + theName + ']').val()},

Categories