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):
<?phprequire_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;?>
But I am getting always back that the user is available, is there anything wrong in the mockjax function I have?
AJAXjQueryPHP
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?
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:-) )
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");
?>
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.
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.