First i echo images for which i set ID with php function. Now im tyring to get the ID value with jQuery click function and pass it to another php function, but unfortunately i always get the undefined index error. Can the reason be in xampp config or something else? Becasuse the code seems to be "right".
jQuery
$(function postImgId() {
$('img').click(function() {
var imgId = $(this).attr('id');
$.post('functions.php', {postId:imgId},
function(data) {
$('#content').html(data);
console.log(imgId);
});
});
});
php
function showPlayer() {
$number ='';
$number = $_POST['postId'];
if(isset($_POST['postId'])) {
echo "<h2>";
echo $_POST['postId'];
echo "</h2>";
}
}
Try:
Put this in your click function:
var imgId = $(this).attr('id');
$.ajax({
type: "GET",
url: "functions.php",
data: 'playerId=' + imgId,
success: function(data){
$('#content').html(data);
}
});
now check with isset($_GET['playerId']) and so on in your php:
if(isset($_GET['playerId'])) {
echo "<h2>";
echo $_GET['playerId'];
echo "</h2>";
}
This playerId should not be something important in your database like the unique AI id.
Whole thing also works with POST see: http://api.jquery.com/jquery.post/
Related
I'm trying to call a PHP page passing parameters with AJAX. I can see that the page is called, but the $_REQUEST doesn't take any parameters. If anyone can help me, I'd appreciate it. Thanks!
My AJAX is that:
<script>
$(document).ready(function(){
$('.c_x').change(function(){
var href = $(this).val();
$.ajax({
type: "POST",
url: "utils/inc_iscrizione.php",
data: 'cod_evento=' + href,
success: function (data) {
alert("data:" + data);
}
});
})
});
</script>
In the PHP Page, I have this.
if(isset($_REQUEST["cod_evento"]))
{
$search = $_REQUEST["cod_evento"];
echo "<script>alert('OK');</script>";
}
else
{
$search = "";
echo "<script>alert('FAIL');</script>";
}
Always the answer is failure. Thank you!
1- change your data format in ajax request from data:'cod_evento=' + href, to data : {code_evento:href}
2 - change your php echo from echo "alert('OK');" to echo "OK"; and same for else clause
Recommended
1 - change $_REQUEST to $_POST
2 - change alert('data:'+ data) to console.log(data)
array_push returns only last value that has been currently pushed into the array in php. Please help. Here is the link where I am working http://dev.optimizebusinessgrowth.com/gopaperboy/members/
Its an jquery ajax. I call ajax with
jQuery(function(){
jQuery('.icon-heart').click(function(){
var user_IDs = jQuery(this).attr('id');
var current_IDs = <?php echo $cui ?>;
jQuery.ajax({
url: '<?php bloginfo('url') ?>/',
type: 'POST',
data: 'ajaxreturn=101&id='+user_IDs+'&myid='+current_IDs,
success: function(result){
jQuery('.testres').html(result);
}
});//ajax ends here
});
});
and here is the code in function.php which gives response
function my_ajaxreturn_101() {
$current_fav = array();
if($_POST['ajaxreturn']==101) {
$userID = $_POST['id'];
$loggedID = $_POST['myid'];
array_push($current_fav,$userID);
print_r($current_fav);
exit;
}
}
Try to use append() instead of html(), so you can do:
jQuery('.testres').append(result);
instead of:
jQuery('.testres').html(result);
Ok , I am trying to get different id values through Jquery , and pass them to Jquery AJAX that will hit a PHP file so I can get some data back .... I'm not sure how to get all the multiple different ids because Jquery is only getting the first id of many of the unique id values generated by the while loop .. and I would like each unique ID to also be passed to the AJAX function in Jquery .. Your help would be so much appreciated . I'm still new to the Jquery world
<?php
require('../database/connection.php');
?>
<script type="text/javascript">
jQuery(document).ready(function() {
var ID = $('div#opposition img').attr("id"); alert(ID);
$.ajax({
type:'GET',
url :'get_users_images.php',
data:'screen_name='+ ID,
success: function(result){
$('div#opposition img').attr('src', result);
}
});
});
</script>
<?php
$select2 = "SELECT * FROM AUTHORS WHERE ID <> $id";
$result2 = mysql_query($select2);
$result_count = mysql_num_rows($result2);
echo '<div id ="opposition">';
while ($row2 = mysql_fetch_array($result2, MYSQL_ASSOC)) {
echo "<img id ='".$row2['Twitter']."' src='images/ajax-loader.gif' class ='image".$row2['Twitter']."'/>"; // echos different ids,
}
?>
</div>
You can send an stringified array of id's like this -
jQuery(document).ready(function () {
var ID = $('div#opposition img').map(function(){
return this.id;
}).get();
$.ajax({
type: 'GET',
url: 'get_users_images.php',
data: { screen_name : JSON.stringify(ID)},
success: function (result) {
$('div#opposition img').attr('src', result);
}
});
});
If I am correct, Actually , why you are placing the set of images returned by php in one
img tag, $('div#opposition img').attr('src', result); . Rather I think you must do something like $('div#opposition').innerHTML(result) .
I am trying to pass a variable from my ajax to my php script. I can't see to get it to work. It keeps on giving me NULL when I do a var_dump on the variable.
JQUERY:
$(document).ready(function() {
$('.trigger').click(function() {
var id = $(this).prev('.set-id').val();
$.ajax({
type: "POST",
url: "../modules/Slide_Show/slide_show.php",
data: id
});
LinkUpload(id);
function LinkUpload(id){
$("#link-upload").dialog();
}
});
});
</script>
PHP:
$id = $_POST['id'];
$query = mysql_query("SELECT * FROM xcart_slideshow_slides where slideid='$id'")or die(mysql_error());
$sli = mysql_fetch_array($query);
$slide_id = $sli['slideid'];
$link = $sli['link'];
var_dump($id);
I need the $id variable to post so I can dynamically change the dialog box when the click function is activated.
EDIT:
So I have changed some of my coding:
Jquery:
$(document).ready(function() {
$('.trigger').click(function() {
var id = $(this).prev('.set-id').val();
$.post(
"slide-show-link.php",
{ id: id },
function(data,status){alert("Data: " + data + "\nStatus: " + status); }
);
// alert(id);
LinkUpload(id);
});
function LinkUpload(id){
$("#link-upload").dialog();
}
});
I wanted to see if the data was in fact being passed so I threw an alert in the .post. This is the error I'm getting now:
I have tried passing plain text and echoing it back on the page but it fails. It is just not passing.
Try this -
$.ajax({
type: "POST",
url: "../modules/Slide_Show/slide_show.php",
data: { id : id }
});
I am doing a delete on a table using jquery,
$('table#chkbox td a.delete').click(function()
{
if (confirm("Are you sure you want to delete this row?"))
{
var id = $(this).parent().attr('id');
var parent = $(this).parent().parent();
$.ajax(
{
type: "POST",
url: "<?php echo base_url().'index.php/libraryController/librarydelete' ?>",
data: { 'id': id },
cache: false,
success: function()
{
parent.fadeOut('slow', function() {$(this).remove();});
}
});
}
});
I am getting the id value correctly but my data parameter doesn't get passed to my controller,
function librarydelete($id)
{
$del = $id;
echo $del;
$this->librarymodel->deletebook_issue($id);
$this->session->set_flashdata('response','Deleted successfully');
redirect('libraryController/loadmagazinedetails');
}
Any suggestion...
I am getting the error,
Missing argument 1 for libraryController::librarydelete() and Undefined variable: id
Your are posting the data, so you can get the id like this:
function librarydelete()
{
$del = $_POST['id'];
echo $del;
$this->librarymodel->deletebook_issue($_POST['id']);
$this->session->set_flashdata('response','Deleted successfully');
redirect('libraryController/loadmagazinedetails');
}
Looks like you are using codeigniter, so this would be even better:
$del = $this->input->post('id');
Change id to $id in controller function parameter
means, your function should be
function librarydelete($id)
{
$del = $id;
echo $del;
$this->librarymodel->deletebook_issue($id);
$this->session->set_flashdata('response','Deleted successfully');
redirect('libraryController/loadmagazinedetails');
}
Perhaps it is because you are using the variable name 'data' twice. Try renaming var data to var mydata.
The way you are passing data is wrong. This is the way you should be setting data.
var data= {id: id };
in your case let me not use the variable in the post action and show you how its done.
$.ajax(
{
type: "POST",
url: "<?php echo base_url().'index.php/libraryController/librarydelete' ?>",
data: {id:id},
cache: false,
success: function()
{
parent.fadeOut('slow', function() {$(this).remove();});
}
});
Edit: After you modified your code to pass data the right way.
Hey I have not followed MVC framwework in php. but since you are doing a post on the action you can always get the data the following way:
$id=$_POST['id']
and then you can use this id.
Also if you still want to use the function:
function librarydelete($id)
{
$del = $id;
echo $del;
$this->librarymodel->deletebook_issue($id);
$this->session->set_flashdata('response','Deleted successfully');
redirect('libraryController/loadmagazinedetails');
}
just modify the uri action to existingurl+/ i.e append the id to the end and in your route map (if there exists something like this in php in .nets implementation there is 1) just add that route.