Send variable from PHP to JQUERY to other PHP file - php

I have an image and after clicking this image I take this game id by JQuery code and then a new PHP tap opens this PHP take should display this image id
Here is the JQuery code:
$(".selected").click(function(){
$.post("bookinfo.php", { id: $(this).attr('id') }, function (response) {
alert(response);
});
});
and here is the PHP code
<?php
$id = $_POST['id'];
echo $id;
?>
and here is the error that i get
Notice: Undefined index: id in C:\xampp\htdocs\bookstore\bookinfo.php on line 2
I have tried AJax request and get but the same error happens..
I have been trying for this error for about 2 hours so please help me!

You can do something like this:
Edited:
jQuery
<script>
$(document).ready(function(){
$(document).on('click','.selected',function(){
var id = $(this).attr('id');
window.location.replace("bookinfo.php?id=" + id);
});
});
</script>
PHP
<?php
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
echo $id;
}
?>

in your php you have to return a json to get it from AJAX. just try changing your php code like this.
<?php
header('Content-Type: application/json');
if(isset($_POST['id']))
{
$data = array($_POST['id']);
echo json_encode($data);
}

Related

session is not storing value for the first time

I am unable to print data in the first run but can get it to print in the second run.I want to print data in first run itself.
i have 3 files
1. main files sends ajax post,print data(main.php)
2. put sent data into session(put.php)
3. print data into excel(print.php)
main.php
<?php
session_start();
echo '<p class="first">ABC</p>'; //DOM IS <p class="first">ABC</p>
?>
$(document).ready(function(){
var value = $('.first').text();
myfun();
function myfun(){
$.post("put.php", {"content":value},function(data){
console.log(data);
});
}
window.location.href ='print.php';
});
put.php
<?php
session_start();
$_SESSION["content"] = $_POST["content"];
?>
print.php
// these file print into excel code is simple
<?php
session_start();
header("content-type:application/xls");
$table = '<table><tr><th>Name</th></tr>';
$table .='<tr><td>'.$_SESSION["content"].'</td></tr></table>';
echo $table;
header("content-Disposition: attachment; filename = new.xls");
?>
please help me,thanks in advance.
Why
js won't stop exec when ajax not complete,
so you should redirect after ajax complete to make sure $_SESSION["content"] has set
Fix
change
$(document).ready(function(){
var value = $('.first').text();
myfun(); //i was missing then
function myfun(){
$.post("put.php", {"content":value},function(data){
console.log(data);
});
}
window.location.href ='print.php';
});
to
$(document).ready(function(){
var value = $('.first').text();
myfun(); //i was missing then
function myfun(){
$.post("put.php", {"content":value},function(data){
console.log(data);
window.location.href ='print.php';
});
}
});

can getJSON() be used for php?

This is the JQuery code loaded over click but the callback function isn't working:
$.getJSON("load_img.php", {"id":"start"} , function(json){
alert(json);
});
PHP code gives me this output:
[
{"img_name":"shiva.jpg","img_id":"1"},
{"img_name":"shiva.jpg","img_id":"2"},
{"img_name":"Recoverd_jpg_file(4).jpg","img_id":"3"},
{"img_name":"Recoverd_jpg_file(542).jpg","img_id":"4"}
]
I cannot load any PHP pages from getJSON() function...i downloaded sample source code from [here][1]
[1]: http://www.sitepoint.com/ajaxjquery-getjson-simple/ and have run it on browser but it doesnt work still...
any help would be much appreciated!!!Thank you
I got my answer today...no <html> tags should be included in the code to be outputted... it should only contain starting and ending php tags i.e.
<?php
//your code or codes
?>
$.getJSON("path to php file", {method: "*", "id":"start"})
.done(function (data) {
console.log(data)
});
//================in php=================================
if($_REQUEST['method'] == "your method" AND $_REQUEST['id'] ) {
//call your php function
}
//-------------------
//and put your php code in function
//.. i hope this help you
<button id="try" type="button">Try</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
$.getJSON("load_img.php", {"id":"start"}, function(result){
$.each(result, function(i, field){
alert(field.img_name);
});
});
i is the field name and field is value. Please check and post me back. For further details :-http://www.w3schools.com/jquery/ajax_getjson.asp
Page Two:-
<?php
// ini_set("display_errors", "On");
//header('content-type:application/json');
$id = $_GET["id"];
if ($id == "start")
{
$con=new mysqli("localhost", "root", "rootwdp", "try");
$numbers = array();
$img_arr = array();
$res = $con->query("select img_name, img_id from img");
while ($row = $res->fetch_assoc())
// mysql_fetch_array() passes parameters from 0
{ $numbers[]=$row; }
echo json_encode($numbers);
}
?>

Pass AJAX Variable to PHP

Hi so my over all aim is to click a table get its ID and then use its ID to load another table. So far I am able to get the ID, but when I try to load the second table I get the error
"Undefined index: Projec_ID in C:\xampp\htdocs\abac\ajaxupdate.php on
line 6 "
Here's my Code
AJAX Script(Console prints the rowID so it is getting, the variable I think something is going wrong when trying to pass it?)
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var log = $("#log");
$(".getRow").click(function() {
console.log("Clicked a row...");
rowID = $(this).find("td.idCell").text();
//Print the row ID in the log cell to make sure we got the right one.
log.text("You 1clicked row "+rowID);
console.log("You cl2icked row "+rowID);
//Send the row ID to ajaxupdate.php
$.post("/abac/ajaxupdate.php", { what: "updateRow", Projec_ID: rowID})
.done( function(data) {
var results = $.parseJSON(data);
console.log(rowID );
})
.fail( function() {
console.log("AJAX POST failed.");
});
});
});
</script>
PHP File (ajaxupdate.php) I think there is something wrong here im guessing
<?php
if( (isset($_POST['submit'])) || (isset($_POST['Projec_ID'])) )
{
$Projec_ID =($_POST['Projec_ID']);
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select($db->quoteName(array('CV_ID', 'Classifier', 'Value', 'TP_ID')))
->from($db->quoteName('sessionta'))
//also is the like part right?
->where($db->quoteName('TP_ID') . ' LIKE '. $db->quote($_POST['Projec_ID']));
$db->setQuery($query);
$results = $db->loadObjectList();
//echo $Classifier;
}
?>
$_POST['submit'] will be false unless you set it:
[...] { "what": "updateRow", "Projec_ID": rowID, "submit" : "true"}
Which is why you get the error message "Undefined Index" -!
Furthermore, you can always:
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
die("The data above was sent via POST");
to troubleshoot these sorts of things..
Your problem could be that you are using an || operator and not an && operator.
if( (isset($_POST['submit'])) || (isset($_POST['Projec_ID'])) )
What this means if at some point you pass $_POST['submit'] and not $_POST['Projec_ID'] it will still run this code. giving you the
"Undefined index: Projec_ID in C:\xampp\htdocs\abac\ajaxupdate.php on line 6 "
Try changing your code to:
if( (isset($_POST['submit'])) && (isset($_POST['Projec_ID'])) )
I think Project_ID isn't going as a string..
Try this code.
<script language="javascript" type="text/javascript">
$(document).ready(function() {
var log = $("#log");
$(".getRow").click(function() {
console.log("Clicked a row...");
rowID = $(this).find("td.idCell").text();
//Print the row ID in the log cell to make sure we got the right one.
log.text("You 1clicked row "+rowID);
console.log("You cl2icked row "+rowID);
//Send the row ID to ajaxupdate.php
$.post("/abac/ajaxupdate.php", { what: "updateRow", "Projec_ID": rowID})
.done( function(data) {
var results = $.parseJSON(data);
console.log(rowID );
})
.fail( function() {
console.log("AJAX POST failed.");
});
});
});
</script>
i just enclosed Projec_ID with with " .

javascript:variable value passing from javascript to php

I have a javascript file pet.js. I want to pass a value of variable in test.php. But i can't.
my pet.js is like
$('#pmWorkOrderDetailsPage').live('pageshow', function(event) {
var id = getUrlVars()["id"];
$.get("test.php", { test1: id } );
$.getJSON('pmworkorderdetails.php?id='+id, displaypmWODetails);
});
function displaypmWODetails(data) {
..............code..........
}
My test.php is like
<?php
$ms = $_GET["test1"];
echo $ms;
?>
But it is not working. I tried with Ajax and post method.
It will be best if I can store the variable value on the session in test.php.
Thanks in advance for any help.
1 do not use getUrlVars() it can make site vulnerable to xss
$('#pmWorkOrderDetailsPage').live('click', function(event) {
var id;// get id
$.get("test.php?id="+id,function(data){
var result=$.parseJSON(data);
alert(result["content"])
});
})
test.php
<?php
$id=$_GET['id'];
$data=array();
$data=array("content"=>$id);
echo json_encode($data);
?>

jQuery/Ajax to dynamically load a specific id PHP page

I'm relatively new to jQuery/Ajax and I was wondering what would be the best way of loading via jQuery/AJAX a php page with an ID attached to it.
obviously
<?php $id = $_SESSION[id]; ?>
<script>
var pageUrl = new Array();
pageUrl[1] = "tab1.html?id=<php echo $id;?>";
.....
</script>
makes no sense.
What would be the best way of going about this? Thank you!
Use jQuery's $.get command and send parameters with it.
$.get("tab1.html", { id: <?php echo $id; ?> },
function(data){
alert("Output goes into the variable data: " + data);
});

Categories