Avatar of Ashraf Hassanein
Ashraf Hassanein
 asked on

Mockjax for php script is always getting the worng value

I am using the Jquery validation plugin to validate the username format, however I want to add an extra step to validate if the user already exists in the data base or not,
   So I have created the following php script (Which is working pretty well):
<?php
require_once(dirname(__FILE__).'/db.php');
$HTML='';
if(isset($_POST['username']) && !empty($_POST['username'])){
      $username=$_POST['username'];
      $username="ashraf";
      $query="select username from users where username='".$username."'";
      $res=pg_exec($con, $query);
      $count= pg_num_rows($res);
      if($count > 0){
        $HTML='Username is not available';
      }else{
        $HTML='Username is available';
       }
      }
   else{
      $HTML='Please enter a username';
   }
  echo $HTML;
?>

Open in new window


And I have created the following mockjax script:
        $(document).ready(function() {
                        $.mockjax({
                                url: "useravailable_check.php",
                                type: "POST",
                                response: function(settings) {
                                          var username = settings.data.username,
                                          response="Username is not available";
                                          this.responseText = "true";
                                          if(response!='Username is available'){
                                                this.responseText = "false";
                                        }
                                },
                        });

Open in new window

 And below is the validator :
                var validator = $("#createuser").validate({
                        rules: {
                                 username: {
                                        required: true,
                                        minlength: 6,
                                        remote: "useravailable_check.php"
                                },
                               },
                             messages: {
                                   username: {
                                        required: "Enter a username",
                                        minlength: jQuery.format("Enter at least {0} characters"),
                                        remote: jQuery.format("{0} is already in use")
                                },
                             },

Open in new window

       But I am getting always back that the user is available, is there anything wrong in the mockjax function I have?
AJAXjQueryPHP

Avatar of undefined
Last Comment
Ashraf Hassanein

8/22/2022 - Mon
Ray Paseur

What is "mockjax?"  I've never heard of this.  Have you verified that the AJAX call is working correctly, calling the backend script and returning the data you need?  You can use a simple JavaScript alert() to print out the intermediate variables.
Ashraf Hassanein

ASKER
In the jquery validation you can define different rule to get different responses for the same elements what I understood that the ajax can not be included as a rule for the jquery validation and instead we use the mockjax where we mock the ajax functionality, that is why I am using the mockjax orcan I can use the ajax here?
Ray Paseur

Wow, a lot of moving parts with many things that can go wrong undected.  Let's start with the background script and work up from there.  In the first code snippet on line 8, what is the value of $res?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ashraf Hassanein

ASKER
in this one it should return back the username, and if it exists the count in line 9 is bigger than 1 else the count is less than one.
My target is the user enter a new username then that it is checked in the front end (jquey) that it is not short nor empty then it sends it to the backend to check if it is available or not.
For now the first 2 parts work properly as with Jquery (It only works when you press the submit button and not outof focus , but this a minor issue I will investigate later:-) )
ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ashraf Hassanein

ASKER
Ray I totally agree about the design is a bit mess, it is better to do all on server side, the point that I have started my design assuming I will do all my checks on the client side, but by time it is always appearing an extra check to be done ending up in this situation.
Regarding your troubleshooting indeed it appears that the php has the problem, I execute the php using using a non existing username, twice:
   With pg_exec():
      $res=pg_exec($con, $query);
    I get the following:
       resource(6) of type (pgsql result)
    With pg_execute:
             $res=pg_execute($con, $query);
     I get the following:
         PHP Warning:  pg_execute() expects parameter 1 to be string, resource given in                      useravailable_check.php on line 8
NULL
PHP Warning:  pg_num_rows() expects parameter 1 to be resource, null given in useravailable_check.php on line 10
         
            And here my db.php which I use to connect:
<?php
$db="mydb";
$hostname = "localhost";
$user = "myself";
$password = "password";
$con = pg_connect("host=".$hostname." dbname=".$db." user=".$user." password=".$password) or die("Could not connect database");
?>


  What is your advice?
Ashraf Hassanein

ASKER
It seems that pg_exec exists for something else:
http://scholar.lib.vt.edu/manuals/php/function.pg-exec.html
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

Ha!  I am not laughing at you at all, but this site you are using is ancient history.  I started writing PHP when the current PHP level was PHP3, in the late 1990's.  PHP 5 has been the current level for more than half-a-dozen years, and PHP 5.2 is now considered obsolete.  So anything with PHP3 and PHP4 in the caption is automatically obsolete.

Ditch Virginia Tech's PHP web site.  Instead, choose some learning resources from this article.  I promise that they are much more up-to-date!
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

I'm not exactly sure how to look at the code set right now (playing Santa Claus) but maybe in the morning before the children get up I can have a look.  Otherwise, I will be back to the issues on Thursday.

Best regards, ~Ray
Ashraf Hassanein

ASKER
:-) well I am  totally in mess so that is why I was hunting in the web sites.
I have checked :    

      I have done:
         $res=pg_exec($con, $query);
         $resultArr = pg_fetch_all($res);
         var_dump($resultArr);
     And:
         $res=pg_query($con, $query);
         $resultArr = pg_fetch_all($res);
         var_dump($resultArr);

    So I changed all pg script to pg_query with the assumption (As I can see in the command description it returns false if no record is found):
 
      $res=pg_query($con, $query);
      $resultArr = pg_fetch_all($res);
      if(!$resultArr){
   And honestly I can see it is working but I want your advice from and expert, so what do you think?

     I have also changed my rules in the jquery validator as the previous question and now it is working.
     Only the replace is not (As the previous question) so I replaced it with trim in the server side script and it is working pretty well however I am only curious now why the replace is not working - thank you so much for your help, and sorry for ruining your christmas night.
Ashraf Hassanein

ASKER
As usual Ray is always great, and helpful
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Ray Paseur

Thanks for the points and thanks for using EE.  Glad to see you making progress here :-)
Ashraf Hassanein

ASKER
I hope one day in my life to reach to you experience :-)