0

This is my code, I store on a local storage and I want it to pass to a php file it seems succesful because the alert "alert("Do something with example.php response");" pop-up. But when it directed to the next page there's an error: Notice: Undefined index: datas in C:\xampp\htdocs\final-project\placenameclick.php on line 183

$(".passdata").click(function () {
var currentplaceind = parseInt($(this).data("indes"));
localStorage.setItem("p", currentplaceind);

var data1 = localStorage.getItem("p");

jQuery.post("placenameclick.php", {datas: data1}, function() 
{ 
    alert("Do something with example.php response"); 
    window.location = 'placenameclick.php';
})
.fail(function() 
{ 
    alert("Damn, something broke"); 
}); 

}

heres my code on the placenameclick.php

<?php

$data = $_POST['datas'];
$results = $pdo->query("SELECT * FROM places WHERE place_id = $data");
while ($row = $results->fetch()) {
    include("places.php");
}
?>
jude
  • 1
  • That's because placenamelick.php isn't checking whether the request is a POST or not before trying to read from $_POST - and `window.location = 'placenameclick.php';` causes a GET request to be sent, without any parameters at all. But if you're using AJAX to send a request to placeameclick.php, then there should be no need to also redirect there afterwards. The whole purpose of AJAX is to enable you to stay on the same main page, without any need for postbacks, redirects, etc. Equally, if you want to redirect, don't use AJAX.Maybe you have not really understood what you're trying to achieve. – ADyson Feb 15 '21 at 14:09
  • 1
    Your code is also wide open to [SQL Injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Machavity Feb 15 '21 at 14:12
  • it's alright, its just a project for this sem. – jude Feb 15 '21 at 14:15
  • That's not an excuse. a) you shouldn't trust people not to make mistakes or turn out to be malicious, and b) you shouldn't trust their machines, or the machines of any other people on your network, or unmanned machines such as servers, not to get contaminated with things which could attack your application. Also, parameters massively reduce the risk of syntax errors as well as injection attacks. And they're not hard to use. So if you know about them, there's absolutely no excuse not to be using them. Do things right the first time, and every time. Get into good habits. – ADyson Feb 15 '21 at 14:20
  • Anyway you need to answer about what you're trying to achieve. It's unclear why you redirected to placenameclick.php after you'd already done an AJAX to it. Maybe you really want to get the output from the AJAX call and append it to your current page? – ADyson Feb 15 '21 at 14:21
  • I understand, and thank you for that but I dont have knowledge about that at the moment. it is my first time to make a website. btw I am trying to achieve that when someone click the name of the place(from the database) from place list, it will be directed to a new page where only one place will be there. – jude Feb 15 '21 at 14:25
  • I loop the database so I can have a place list and store the place_Id on the data-* attribute of a button. now I want to get that place_id and put on placenameclick.php to access again the database and choose the place that the user pick – jude Feb 15 '21 at 14:28
  • `I dont have knowledge about that at the moment`...ok well now is the time to find out. Macavity provided you a link which explains it. Get into good habits now, and write your code the correct way. Then when you come to do a more serious project, you'll already know how to do things properly. – ADyson Feb 15 '21 at 14:28
  • `I want to get that place_id and put on placenameclick.php to access again the data base and choose the place that the user pic`...if you want to make that happen using a redirect, then get rid of the AJAX entirely, and make a GET request e.g. `window.location = 'placenameclick.php?datas=' + data1;`. Then in the PHP, replace `$_POST['datas'];` with `$_GET['datas'];` . I'm not sure that you need localstorage either, though. – ADyson Feb 15 '21 at 14:31
  • IT WORKS! thank you so much sir, and your advice are well noted <3 – jude Feb 15 '21 at 14:36
  • Im still learning sir, so I dont know how to have a proper logics but im working on it – jude Feb 15 '21 at 14:40

1 Answers1

1

You're making TWO requests to placenameclick.php.

The first one is a POST request with Ajax and the value is defined.

The second one is a GET request with browser navigation and the value is not.


The point of Ajax is to exchange data without navigation. Since you want to navigate, Ajax is the wrong tool.

Remove the JavaScript. Replace it with a regular HTML form.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • the value from local storage were from the loop of database, I store place_Id on a button using data-* attribute and get it using javascript. is there any other way for this? – jude Feb 15 '21 at 14:14