-1

I have created an HTML page and am attempting to use AJAX via JS to echo from a PHP page:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>User Retrieval</title>
    <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
    <script>
function getid(){
    var userid = document.getElementById('userid').value;
    $.post('Users2.php', {postname:userid},
    function(data){$('#results').html(data);});
    };
    </script>
</head>
<body>
    <h1>User Retrieval</h1>
    <p>Please enter a user ID:</p>
    <input type="text" id="userid" placeholder="Please insert user ID" onkeyup="getid()" />
    <div id="results"></div>
</body>
</html>

I have tested the JS and see that userid indeed gets the information from the HTML.

I then wrote the following PHP:

<?php
if (isset ($_POST['postname'])) {
    $name = $_POST['postname'];
    echo name;
}
else
{
    echo "There is a problem with the user id.";
}

?>

However, I am always getting the else echo statement. What am I missing here?

I am using XAMPP for local host checks.

Nimrod Yanai
  • 777
  • 2
  • 8
  • 30
  • Check in console, network in inspect element if data is there. – Bhaumik Pandhi May 24 '17 at 12:08
  • what error you are getting in console? – Naveen May 24 '17 at 12:10
  • But this inspects my html. Which element am I looking for? – Nimrod Yanai May 24 '17 at 12:11
  • @Naveen there is no error – Nimrod Yanai May 24 '17 at 12:12
  • 1
    echo $name; > This might be the problem – Dilip Belgumpi May 24 '17 at 12:17
  • 3
    *"there is no error"* - Well, if that's your real code, you should have been getting an undefined name constant error along with undefined index notices, but you weren't checking for errors. This of course will happen once it goes into that condition. – Funk Forty Niner May 24 '17 at 12:18
  • Where do you see the else echo statement, in the `#results` div? I just want to check you are not doing something weird like visiting `users2.php` in your browser – Steve May 24 '17 at 12:18
  • @Fred When I say "there is no error" I mean the console does not show any errors. I've had that error but already fixed that using the documentation before posting my question. – Nimrod Yanai May 24 '17 at 12:20
  • @Steve yes, it is in the results div. – Nimrod Yanai May 24 '17 at 12:20
  • Developer console and php are two different animals. – Funk Forty Niner May 24 '17 at 12:21
  • Have you a 301 HTTP response ? (in this case, no error but post var will be skipped) – Théo Bouveret May 24 '17 at 12:22
  • Place a breakpoint in the php file and step through the code. Failing that `var_dump($_POST)` and check in the network tab of the browsers developement tools. Check both the request and response – Steve May 24 '17 at 12:24
  • since you've no form with a post method or any post for the ajax, then the conditional isn't set, that's why and php's error reporting told you about it as you stated *"I've had that error but already fixed"* but you chose to ignore it. – Funk Forty Niner May 24 '17 at 12:25
  • @Fred-ii- `$.post()` initiates an ajax post request? – Steve May 24 '17 at 12:26
  • @Fred so I should place my input textbox in a form with a post method? I thought I only needed that if I use a "submit" action, but here I am taking the value of the textbox directly via JS after each character. – Nimrod Yanai May 24 '17 at 12:27
  • @Steve yeah you're right I overlooked that. TBH, JS isn't my bag since I mostly work serverside. – Funk Forty Niner May 24 '17 at 12:27
  • @Fred-ii- fair enough. TBH these type of questions always feel like pulling teeth! I wish debugging was given more emphasis for new programmers. – Steve May 24 '17 at 12:29
  • 1
    Copied the code directly, only change was the `$name` in Users2.php and it all works perfectly for me. Maybe your PHP file is not in the same directory as your HTML? – fez May 24 '17 at 12:30
  • @NimrodYanai There are no obvious issues (beyond `name $name` already mentioned). Only you have the access to work this one out. I look into the network tab would be a very good start. – Steve May 24 '17 at 12:31
  • @Steve Well Steve, they have enough to go on in regards to debugging. I'd of liked to have been more help to them with the JS stuff. The developer console and php's error reporting alone should have been ample enough tools for them to fix this. There is definitely something not passing through their POST arrays, one of which being no "name" attributes; that's the serverside issue I can see. – Funk Forty Niner May 24 '17 at 12:31
  • @FezWas You are absolutely right, the path was wrong! Wow that was a silly mistake! – Nimrod Yanai May 24 '17 at 12:34
  • @NimrodYanai What was the http response code before the correction ? – Théo Bouveret May 24 '17 at 12:43
  • Surprisingly there was no http response code that I saw, it was just going straight to the "else" statement. Odd. – Nimrod Yanai May 24 '17 at 12:47
  • 1
    There is always a http status* code when you do an http request. https://en.wikipedia.org/wiki/List_of_HTTP_status_codes Your issue looks like an 301/302 code. You reach your php file without the right path and your POST array is empty because of that. Result : No error catch in the post javascript function ( no http error status code 4xx or 5xx ) and no error in php script, just an empty POST array. That's why using the right path naturally correct the issue. Got the similar problem recently because of a slash at the end of the url ... – Théo Bouveret May 24 '17 at 14:08

2 Answers2

-2

Try this, It might help

    <?php
    if ($_POST[])  {
        $name = $_POST['postname'];
        echo $name;
    }
    else
    {
        echo "There is a problem with the user id.";
    }

    ?>
Keshari Nandan
  • 1,040
  • 9
  • 22
  • I did just to be sure, but the message I'm getting is from the "else" section, meaning the browser jumped over the IF as false, so it never even got to that line anyway :\ – Nimrod Yanai May 24 '17 at 12:18
  • not my downvote here but that is only a partial answer. There's a bit of work to be done to their code to get it going. Once that `if` kicks in, that's where the trouble starts and will keep going. – Funk Forty Niner May 24 '17 at 12:20
  • your edit *"Try this"*, is still a "Try this"; please explain it so that everyone who doesn't know what's wrong, will understand. – Funk Forty Niner May 24 '17 at 12:22
  • Instead of checking a particular key inside the $_POST[] array, I will suggest to check if the method is post and then we can check if that particular key exist or not. Sometime it happen that ajax is working but that key inside the $_POST is not getting a value from Javascript then isset function returns false. – Keshari Nandan May 24 '17 at 12:26
  • Keshari you were right with the $name, the reason it didn't work was a wrong path to begin with! Thank you! – Nimrod Yanai May 24 '17 at 12:35
  • 1
    @NimrodYanai *"the reason it didn't work was a wrong path to begin with!"* - Your accepting this answer and what you posted for a question does not support anything here, IMHO. – Funk Forty Niner May 24 '17 at 12:39
  • I have no other explanation. I changed to $name, didn't work, correct the path, and it worked. I assume those were the two issues. – Nimrod Yanai May 24 '17 at 12:48
-2
var userid = $("#userid").val();
$.ajax
({
    type:'post',
    url:'user2.php',
    data:{
            get_id:"user2.php",
            userid:userid,
        },
            success:function(data) {
        if(data){
            $("#results").html(data);
        }
});

Php File

<?php
if (isset ($_POST['userid'])) {
    $name = $_POST['userid'];
    echo $name;
}
else
{
    echo "There is a problem with the user id.";
}

?>