0

The script I have in my HTML file is printing the ajax script instead of an actual message. HTML script:

<script src="{{url_for('static', filename='jquery-3.6.0.min.js')}}"></script>
      <script src="{{ url_for('static', filename='sign_up.js')}}"></script>
      <script>
      $(document).ready(function(){
         $("#p3").keyup(function(){
            var username = $(this).val().trim();
            if(username != ''){
               $.ajax({
                  url: '../static/ajaxfile.php',
                  type: 'GET',
                  data: {username: username},
                  success: function(response){
                      $('#uname_response').html(response);
                   }
               });
            }else{
               $("#uname_response").html("");
            }
          });
       });
      </script>

ajaxfile.php

<?php
include "config.php";

if(isset($_POST['username'])){
   $username = mysqli_real_escape_string($con,$_POST['username']);

   $query = "select count(*) as cntUser from users where username='".$username."'";

   $result = mysqli_query($con,$query);
   if(mysqli_num_rows($result)){
      $row = mysqli_fetch_array($result);

      $count = $row['cntUser'];

      if($count > 0){
          $response = "<span style='color: red;'>Not Available.</span>";
      }else{
        $response = "<span style='color: green'>Available.</span>";
      }

   }

   echo $response;
   die;
}
?>

The output on the webpage is "0){ $response = "Not Available."; }else{ $response = "Available."; } } }" Under the username text box. The username textbox has an id of "username", and I checked my terminal, its querying the ajaxscript.php file with the username data, but just getting an incorrect repsonse.

Roark
  • 69
  • 6
  • _incorrect repsonse_ what is the response you are getting? – endeavour Jun 26 '21 at 04:30
  • What is on the webpage output, since I am putting that response into the element with id "uname_response". Its a section of the ajaxfile.php code. – Roark Jun 26 '21 at 04:32
  • it looks like you're echoing php that is in single quotes – Kinglish Jun 26 '21 at 04:40
  • 2
    It looks as if there is no PHP running on your server, your Webserver either doesn't support it or it's misconfigured. Please describe your webserver environment. Actually what you get is the _full_ code (it's just treated as regular file and its contents are returned) as you can see on your browser devtools' network tab, the reason you only see a part is because the whole part from the `` (where you happen to have a `>`) is interpreted as an HTML tag. – CherryDT Jun 26 '21 at 04:50
  • 1
    ... Actually, I just realized you are attempting to execute a PHP file located in a folder called `static` which sounds like it should contain _static_ files, not dynamic code, so probably your framework is intentionally not executing PHP there (it may even be optimized to be served directly by the reverse proxy or even a CDN). Put the PHP file next to the other PHP files in your project outside of the `static` folder and see if that helps. – CherryDT Jun 26 '21 at 04:55
  • 3
    ... _Actually_, now I'm thinking the template you showed looks like the whole thing is a Flask application which isn't even PHP, it's Python... Maybe you got something mixed up there? My gut tells me you looked at a bad tutorial about AJAX and it assumed the whole world uses PHP, and you attempted to apply it to your application that's written in Python, which won't work. – CherryDT Jun 26 '21 at 04:57
  • Yep thats right, my server is running on Flask in Python. I was trying to implement a php script here to access a database i have to check if the username entered on the page is unique or not. Since I've done the server side in Flask, is my only option to check for this after the post request? Or is there a way to access the database on a keyUp event like ive done in the html script, using ajax? – Roark Jun 26 '21 at 07:50
  • I have pretty much no experience with ajax or php, but willing to learn it to implement it here if I can achieve this. The keyup event seems to be trigerring, its just not able to access my database and check the username against it. – Roark Jun 26 '21 at 07:52
  • 1
    If you are using `flask` with `python` why do you want to use `php`? I do not work on python myself but cant you create a route to a function in `python` that will do the task for you. Ajax is not limited to making calls to php. It simply processes a get or a post request to the url you ask it to. What you do on the backend and how you do it is not known by ajax (which works on JS) and is client side. – endeavour Jun 26 '21 at 08:34
  • 1
    Since your application is a flask application which is based on python, you also need to write the server code which responds to your Ajax request in python, not PHP. Using two different server side languages makes no sense and won't work well, as you've discovered. Plus it means the code can't integrate with any other services and libraries your existing application already provides – ADyson Jun 26 '21 at 09:27
  • Because I want to have a function run on the page, not post request. I dont want the webpage data to travel thru a post request to python backend, then check for username uniqueness, and return the form again on fail. I want to check the username against the database as the user is typing it, thats why i used to keyup event on the username textbox. Im thinking this is possible with flask, I just have to pass the usernames list, from the db, into the html file using render_template. Trying it now, but not sure if this is the correct way to do this. My server code is all in python. – Roark Jun 26 '21 at 09:29
  • `thats why i used to keyup event on the username textbox`...that's fine. But it has nothing to do with choosing PHP or python. The keyup event happens using JavaScript. You can then still send an Ajax request to your python code instead of sending it to a PHP script. – ADyson Jun 27 '21 at 06:59

1 Answers1

0

You send GET request but on the server side you are checking for POST username, probably will not fix the asked question but it is a step closer

Joro
  • 21
  • 4
  • I had "POST" before but it said method not allowed. I just hcanged the method in ajaxfile.php to _GET and still same error. – Roark Jun 26 '21 at 04:35
  • @Roark That is just fiddling around. Figure out why [post is not allowed](https://www.google.com/search?q=php+post+method+not+allowed+site:stackoverflow.com) and use POST – mplungjan Jun 26 '21 at 04:39
  • What is the output if you replace if else block with some hardcoded value – Joro Jun 26 '21 at 04:48
  • It just prints that hardcoded value. I think the issue is that I was using php in combination with Flask, which cannot work. I wasn't aware of this previously, so I am going to try to implement this by passing the list of usernames in the database to the html page via the render_template() function in python and then use a script in html to check there. – Roark Jun 26 '21 at 08:21