pass ID from jQuery to PHP [duplicate] - php

This question already has answers here:
Send values to $_GET using jQuery
(4 answers)
Closed 9 years ago.
HTML
Link1
Link1
Link1
jquery
function mainFunction() {
$(".leftPanel").click(function () {
$(".leftPanel").pageslide({
direction: "right",
modal: true
});
var linkId = $(this).attr('id');
var linkId2 = (linkId.substring(1, linkId.length));
console.log(linkId);
console.log(linkId2);
$.ajax({
type: 'GET',
url: 'my.php',
data: linkId2,
success: function (data) {
console.log(data);
}
});
return false;
});
}
And my PHP
<?php
$pageid = $_GET['linkId2'];
print_r($_GET);
?>
So this is my code but it seems not working..Had looked around dint found anything that can help.
Explain the script
I want to take ID of a link and assign it to $pageid
The console.log(data); Saying Access Denied
print_r($_GET); Saying array();
If any suggestion with code.
If need more details please ask I will explain everything that I can.
Will appreciate any help. Thank you a lot.

There is an error in your jquery.ajax function. You missed to pass an identifier for the data. Use this:
data: {"linkId2" : linkId2},

Related

Cannot get value of implode() response from php file using ajax (array to string conversion error) [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 months ago.
The scenario is here I accept all button click that has ID starts with 'editbtn' like editbtn1/editbtn2 etc ; and get only the number to send via ajax to find data id based on the button id (number)
$(document).ready(function(){
$('[id^=editbtn]').click( function () {
noform=this.id.match(/\d+/);
event.preventDefault();
$.ajax({
'url': 'visitor_book_crud.php',
'data': {
'request': 'find_data',
'data_id': noform
},
'type': 'post',
'dataType': 'html',
'beforeSend': function () { }
})
.done( function (response) {
console.log(response);
})
.fail( function (code, status) { alert(code+' - '+status);
})
.always( function (xhr, status) { })
});
});
And In visitor_book_crud.php
if ($request=="find_data"){
$data_id = $_REQUEST['data_id'];
$mqdata = mysql_query("SELECT * FROM tb_visitorbook WHERE id_vb = '$data_id'") or die (mysql_error());
$mfadata = mysql_fetch_assoc($mqdata);
if($mfadata){
echo implode(",", $mfadata);
} else {
echo "failed";
}
}
I tried to directly send request to visitor_book.crud?request=find_data&data_id=1 and the output is like this, exactly same as what I want to be appeared in ajax response
1,2022-06-29,03:07:30,03:39:39,6,,A_NAME,3,,,SOME_NAME,,1,
But when I press edit button, it says
<br />
<b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\security_editless\visitor_book_crud.php</b> on line <b>48</b><br /> //line 48 is in mysql_query("SELECT......
failed
I searched from many thread but still dont solve my problem, any help would be appreciated
First of all, you should do all the validation before sending the data from JS and also validate the data on the PHP side as well.
However noform = this.id.match(/\d+/); will give your an array. to get the number you'll have to use noform[0], also make sure you do the validation if any match found or not before using noform[0]
right now you're sending noform as data_id and your PHP is warning about it.

How can i use php variable from js? [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 3 years ago.
I have several products. On click product, I send "id" of clicked product to Local Storage. To use this variable in php I send it to items.php ($.post).
Then I need use this "id" from items.php to show in the cart, but variable is emply.
var LSArray = JSON.parse(localStorage.getItem('productID')) || [];
function clickOnProduct(id) {
var newItem = {'id': id};
LSArray.push(newItem);
localStorage.setItem("productID", JSON.stringify(LSArray));
var dataLS = JSON.parse(localStorage.getItem('productID')) ;
$.ajax({
type : "POST",
url : "/wp-content/themes/twentynineteen/items.php",
data : { name : dataLS[dataLS.length-1].id },
success: function(res){
console.log(res);
}
});
}
All works fine, but how can I use "id"?
<?php incude(items.php); ?>
dosn't work.
You are sending it by post. So you can take a look to your post variable by
<?php var_dump($_POST); ?>
Then you will see the array and probably you can access your data by
$_POST['name']

Why ajax loaded link not clickable on jQuery click event? [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 5 years ago.
I called Ajax on button click, and I got response with link. Now I want to do some action on that link but I cant, why?
here is the ajax call:
$( document ).ready(function() {
jQuery(".rateReset").click(function(){
var data_item = jQuery(this).attr("data-item");
var data_target = jQuery(this).attr("data-target");
var me = this;
jQuery.ajax({
type: "POST",
url: "likevoting.php?data_item="+data_item+"&data_target="+data_target+"&interchange=yes",
success: function(data){
jQuery(me).closest("div.rateWrapper").html(data);
}
});
});
I got response like:
some text link lorem ipsume...
Now I want to add click event on 'link' text, how to do this.
Thanks
replace
jQuery(".rateReset").click(function(){
to
jQuery(document).on("click", ".rateReset", function(){

Set Session inside the javascript [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I want to set session inside the javascript. But all the time this function return false value. Please tell me the error of this code.
<script>
function myFunction(){
var r=confirm("Would you like to add another item ?");
if (r==true) {
alert('ok');
var varname = '<?php echo $_SESSION["redirect"]=1; ?>';
}
if(r==false) {
alert('bad');
var varname = '<?php echo $_SESSION["redirect"]=2; ?>';
}
}
</script>
You can't evaluate PHP on the client. Do an AJAX request to the server setting the session variable instead.
Javascript runs on the client side, so you'll need to perform an AJAX call to a server side script that will change the sessions value.
Something in the lines of:
function changeSession()
{
$.ajax({
url: /change_session.php?value=1&key=redirect,
dataType:"html"
});
}
And in change_session.php:
<?php $_SESSION[$_REQUEST['key']]=$_REQUEST['value'] ?>

Need to pass a value from PHP to Javascript [duplicate]

This question already has answers here:
how to pass these strings from php to javascript
(5 answers)
Closed 9 years ago.
I need to pass a value from PHP back to my javascript. I have tried so many things but can't get it to work. Here is my code.
<script type="text/javascript">
life = {};
life.anjaxEnter = function(finished) {
$.ajax({
dataType:"json",
type:"POST",
data:mydata,
url:"myurl.php",
async:false,
success:life.receiveResult,
});
setTimeout("location.href='myredirectionurl'", 1000); // Redirect after 1 second
}
life.receiveResult = function(result) {
alert(result);
}
</script>
I need to pass a specific value from the PHP file back to life.receiveResult and then add this to the redirection url.
The biggest problem is probably that at the time when you assign life.receiveResult to success, life.receiveResult is undefined. You need to define it before you assign it to something else.
myurl.php needs to return a JSON result when it's called. Something like this:
<?php
header("Content-Type: text/json");
$mydata = "Hello";
echo json_encode(array('ok' => true, 'mydata' => $mydata));
?>
Have a look at the json_encode docs, the header docs and some of the other answers for more information.

Categories