I am usign jason_encode in php_ajax file, but I am having error message in consol, "Ajax error: 200 parsererror", Can You please review my code ??
I have one index file that show the status message on runtime using ajax and jason, this is my ajax file that send the true and false to my index with error message if exist otherwise it show the ok message if each check goes good.
$validateValue=$_REQUEST['fieldValue'];
$validateId=$_REQUEST['fieldId'];
$emailaddress = $validateValue;
$validateError= "email is not correct";
$validateSuccess= "email is correct";
$arrayToJs = array(); // creating array
$arrayToJs[0] = $validateId;
$arrayToJs[1] = $validateId;
if($numrow > 0) // where $numrow is getting data from database.
//if($validateValue !=="raza#gmail.com") // this check can also be used
{ // validate??
for($x=0;$x<1000000;$x++){
if($x == 990000){
$arrayToJs[1] = false;
echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR
//return false;
}
}
}
else
{
$arrayToJs[1] = true; // RETURN TRUE
echo json_encode($arrayToJs); // RETURN ARRAY WITH success
## Check for the ggg mail
}
if($validateValue == "ggg#gmail.com")
//check the second check
{ // validate??
for($x=0;$x<1000000;$x++){
if($x == 990000){
$arrayToJs[2] = false;
echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR
//return false;
}
}
}
else
{
$arrayToJs[2] = true; // RETURN TRUE
echo json_encode($arrayToJs); // RETURN ARRAY WITH success
}
Please Try This
$validateValue=$_REQUEST['fieldValue'];
$validateId=$_REQUEST['fieldId'];
$emailaddress = $validateValue;
$validateError= "deze naam wordt geblokkeerd";
$validateSuccess= "valid name";
/* RETURN VALUE */
$arrayToJs = array();
$arrayToJs[0] = $validateId;
## check email Already Exist
if($numrow > 0 ) // this should return your value from database
//if($validateValue !=="raza#gmail.com")
{ // validate??
for($x=0;$x<50;$x++){
if($x == 9){
$arrayToJs[1] = false;
echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR
return false;
}
}
}
### Second Step
/// if you will use elseif instead of sigle if else statement it will work because it get only one message in array in a single time.
elseif($validateValue == "ggg#gmail.com")
{ // validate??
for($x=0;$x<1000000;$x++){
if($x == 990000){
$arrayToJs[1] = false;
echo json_encode($arrayToJs); // RETURN ARRAY WITH ERROR
return false;
}
}
}
else
{
$arrayToJs[1] = true; // RETURN TRUE
echo json_encode($arrayToJs); // RETURN ARRAY WITH success
}
Related
For true I'm receiving "Successfully UNENLISTED(1)!" and for false I'm receiving "Successfully ENLISTED(1)!".
dump($swap) will return true and still execute the IF else while if it returns false it still execute as true
Am i not understanding something specific to CakePHP?
//to grab database entry
public function swapSub($user_id_main, $user_id_from) {
$result = $this->find()
->where(['Enlist.user_id_main'=>$user_id_main,'Enlist.user_id_for'=>$user_id_from])
->first();
dump($result);
if($result['active']){
$result['active'] = 0;
if($this->save($result)){
return false;
}
} else {
$result['active'] = 1;
if($this->save($result)){
return true;
}
}
}
//display true and false statement. //$swap == TRUE/does not work either
$swap = $this->Enlist->swapSub($user_id_main, $user_id_from);
dump($swap);
if($swap){
$this->Flash->success(__('Successfully ENLISTED(1)!'));
} else {
$this->Flash->success(__('Successfully UNENLISTED(1)!'));
}
I have a html form and this form is validating by PHP with jQuery/Ajax request.
Currently it's working perfectly. Now I want to show a dynamic error message.
For e. g:
I am validating integer number using following function :
function only_number ($string) {
if( preg_match('/^[0-9]+$/', $string ) ) {
return true;
} else {
return false;
}
}
This function is implementing by following way :
$msg = array();
$msg['error'] = false;
if(empty($emp_id)) {
$msg[] = 'Select assign to';
$msg['error'] = true;
} elseif( only_number($emp_id) === false ) {
$msg[] = 'Assign to must be numeric value';
$msg['error'] = true;
}
echo json_encode($msg);
You see that I am showing error message
Assign to must be numeric value
in the function implemented page e.g. update.php
It's very time consuming that I need to type several type of error message every time.
NOW, I want to write this error message in the function and it will show/implemented on validating page like : update.php page.
How can I do this ?
You can return array from your function. E.g:
function only_number ($string) {
$result = array(
'success' => false,
'error' => '',
);
if( preg_match('/^[0-9]+$/', $string ) ) {
$result['success'] = true;
} else {
$result['error'] = 'Assign to must be numeric value';
}
return $result;
}
And check:
if (empty($emp_id)) {
$msg[] = 'Select assign to';
$msg['error'] = true;
} else {
$check = only_number($emp_id);
if ($check['success'] == false) {
$msg[] = $check['error'];
$msg['error'] = true;
}
}
In stead of returning true/false you can also return a string with the error message
function only_number ($string) {
if (empty ($string) return 'Select assign to';
if(!preg_match('/^[0-9]+$/', $string ) ) return 'Assign to must be numeric value';
else return 'no error';
}
next in your code
if (only_number($emp_id) <> 'no error') echo (json_encode(only_number($emp_id)));
This is a sample of individual functions that validate form data from a request submission. A variable of true has been set and each function checks for validation requirements then either continues without returning anything or returns false and changes the $check value. The function down the bottom then checks if the $check value has changed to false and if it has the SQL statement will not be run.
$check = true;
function productNameValidation(){
if(isset($_REQUEST['product_name']) && !empty($_REQUEST['product_name']) && preg_match("/^[A-Za-z0-9 :]*[A-Za-z0-9][A-Za-z0-9 :]{0,50}$/",($_REQUEST['product_name']))){
//then $valid['ID'] = "string: " . $_REQUEST['ID']
$valid['product_name'] = $_REQUEST['product_name'];
$err['product_name'] = "No errors";
//if not
} else {
if(empty($_REQUEST['product_name'])){
$valid['product_name'] = "No data entered!";
} else {
$valid['product_name'] = $_REQUEST['product_name'];
} //$err['ID'] = "error message"
$err['product_name'] = "Product Name must only contain letters, numbers and ':'!";
$check = false;
}
}
function checkProduct()
{
productNameValidation();
productGenreValidation();
productPriceValidation();
productEsrbValidation();
productThumbnailValidation();
releaseDateValidation();
return $check;
}
if($check == true)
{
//Insert into database
}
What you need to do is add different variables on different functions. If you are working this code to the method that it begins as true and is required to be checked and if the check fails then becomes false, try this method:
// $check = true;
function productNameValidation(){
$nameValidation = TRUE;
if(isset($_REQUEST['product_name']) && !empty($_REQUEST['product_name']) && preg_match("/^[A-Za-z0-9 :]*[A-Za-z0-9][A-Za-z0-9 :]{0,50}$/",($_REQUEST['product_name']))){
//then $valid['ID'] = "string: " . $_REQUEST['ID']
$valid['product_name'] = $_REQUEST['product_name'];
$err['product_name'] = "No errors";
//if not
} else {
if(empty($_REQUEST['product_name'])){
$valid['product_name'] = "No data entered!";
} else {
$valid['product_name'] = $_REQUEST['product_name'];
} //$err['ID'] = "error message"
$err['product_name'] = "Product Name must only contain letters, numbers and ':'!";
$nameValidation = false;
}
return $nameValidation;
}
function checkProduct()
{
$checkProduct = true; ///true until proven false.
$checkProduct = productNameValidation();
//This code gives $checkProduct the boolean value returned
//from the function
$checkProduct = productGenreValidation();
$checkProduct = productPriceValidation();
$checkProduct = productEsrbValidation();
$checkProduct = productThumbnailValidation();
$checkProduct = releaseDateValidation();
return $checkProduct;
}
if($checkProduct == true)
{
//Insert into database
}
What I have done here is each function returns a TRue/False flag boolean variables which can be checked with an if(){ statement, you can run through numerous functions in this way checking each aspect you need. The important point is that you need to return a value from each function and you can set the booleans manually with initial settings which is then updated upon conditionals - such as setting $checkProduct = TRUE until it is FALSE from any sub function.
Global variables are really not a good idea in this case.
Edit: Thanks to #Edward for some clarification of boolean return code.
You can do something like that:
function productNameValidation(){
$check = true;
if(isset($_REQUEST['product_name']) && !empty($_REQUEST['product_name']) && preg_match("/^[A-Za-z0-9 :]*[A-Za-z0-9][A-Za-z0-9 :]{0,50}$/",($_REQUEST['product_name']))){
//then $valid['ID'] = "string: " . $_REQUEST['ID']
$valid['product_name'] = $_REQUEST['product_name'];
$err['product_name'] = "No errors";
//if not
} else {
if(empty($_REQUEST['product_name'])){
$valid['product_name'] = "No data entered!";
} else {
$valid['product_name'] = $_REQUEST['product_name'];
} //$err['ID'] = "error message"
$err['product_name'] = "Product Name must only contain letters, numbers and ':'!";
$check = false;
}
return $check;
}
if(productNameValidation()) {
....
}
You can return $check in your validation functions which will allow you to use the value of $check outside the function scope like this: $check = productNameValidation(). Another important note which I saw mentioned above: You should try to avoid the global scope as much as possible.
You can use check like a local variable not global, so in function.
Instead if you want it as a global, at the beginning of the function, you have to specify that you referring to
global $check;
I have been debugging some php code today and have run into a very strange problem. A function that I have to check if a password is valid stops executing part way through the function. No errors are generated either by PHP or by the web server itself.
Here is the function in question:
//Common Registration Functions
function checkPassword($password)
{
$bLen = strlen($password);
echo $bLen."\n";
echo $password."\n";
//Remove any illegal characters
$vPWord = preg_replace("/[^\!\#\#\\\$\%\&\*\-\_\,\.a-zA-Z0-9]/","",$password);
$aLen = strlen($vPWord);
echo $aLen."\n";
echo $vPWord."\n";
//If the password length before santization is different than after then the user used illegal characters
if ($bLen <> $aLen)
{
return "pass_charfail";
}
echo "pass length check 1 \n";
//Check sanitized password length
if (strlen($vPWord) < 6)
{
return "pass_short";
}
echo "pass length check 2 \n";
if (strlen($vPWord) > 10)
{
return "pass_long";
}
echo "pass length check 3 \n";
//Check password strength
$strength = 0;
if (preg_match("/[^a-z]/",$vPWord))
{
$strength += 1;
}
if (preg_match("/[^A-Z]/",$vPWord))
{
$strength += 1;
}
if (preg_match("/[^0-9]/",$vPWord))
{
$strength += 2;
}
if (preg_match("/[^\!\#\#\\\$\%\&\*\-\_\,\.]/",$vPWord))
{
$strength += 4;
}
if ($strength > 6)
{
echo $strength."\n";
return true;
}
else
{
echo $strength."\n";
return "pass_weak";
}
}
Here is the output I get from my error checking setup (my webhost will not enable php debugging for an entire site so I have to go through a separate file which I will post the code from later):
4
Mast
4
Mast
{"success":"noerror"}
Here is the way I have to check for errors:
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
include("register.php");
?>
And here is the function which calls the function in question above:
function register($username, $password, $email, $squestion, $sanswer)
{
//First check if the email is valid
$data = eVerify($email);
//If email is not valid
if (!$data)
{
return "email_fail";
}
//If email is valid then check if it already exists and the verification status
else
{
//See if the email already exists
$data = getUID($email,"email",true);
//echo $data."\n";
if ($data)
{
//Get user ID for later use
$id = getUID($email,"email",false);
//If the email exists, see if it has been verified or not
$data = checkVer($id);
//echo $data."\n";
//If email exists but has not been verified
if (!$data)
{
rSVCode($username,$email,$id);
return "exists1";
exit();
}
//If email exists and has been verified
else if ($data)
{
return "exists2";
exit();
}
}
//If email does not exist, continue registration process
else
{
//Check to see if username has been used
$data = getUID($username,"username",true);
if ($data)
{
return "un_exists";
exit();
}
//Check password strength, chars, and length
else
{
$data = checkPassword($password);
if ($data)
{
//Create user account
$data = cAccount($username, $password, $email, $squestion, $sanswer);
if ($data)
{
//Get user's ID for use later
$id = getUID($username,"username",false);
//Generate email verification code
$data = cVCode($username,$email,$id);
//Send verification email
$data = sendEVar($email,$username,$data);
if ($data)
{
return "true";
exit();
}
else
{
return $data;
exit();
}
}
else
{
return $data;
exit();
}
}
else
{
return $data;
exit();
}
}
}
}
}
The triple === makes sure the return is of the same type.
In your function you don't always return boolean, sometimes you return strings, and that could be an issue.
For example this snippet:
$data = "pass_charfail";
if($data){
echo 'true';
}else{
echo 'false';
}
this will echo true because $data is not an empty string.
But the following will echo false, because $data is not a true boolean.
$data = "pass_charfail";
if($data === true){
echo 'true';
}else{
echo 'false';
}
One more example in your register function you have
if ($data)
{
return "true";
exit();
}
if this value gets return, then false will be echo from the following code:
if($data === true){
echo 'true';
}else{
echo 'false';
}
because $data is now a string which is not of type boolean.
hope it makes sense to you!
I got it working again but I am not sure why the change I made makes a difference. If someone could respond to this answer or post their own answer explaining it would be appreciated.
How I fixed it was changing the if ($data) line after checkPassword is called to if ($data === true) and it reported the correct error message instead of claiming a successful registration.
I have a code like this
First looping count how many post the array:
for($i = 0; $i < $jumlah_qty ;$i++) {
if(!empty($qty[$i]) && !empty($id_cat[$i])) {
Insert booking:
$insert_booking_hd = $user_class->select($az);
$id_cates = $id_cat[$i];
for($b = 0;$b<$qty[$i];$b++) {
First validation if $_POST[$id_cates) is set run this code:
if(isset($_POST[$id_cates."".$b])){
$id_seat = $_POST[$id_cates."".$b];
Find the seat number in $select_seat and find if seat number is exist in $seat_number:
$select_seat = $user_class->select($query);
$seat_number = $user_class->select($querys);
$row_seat = $user_class->numrows($select_seat);
$row_seat2 = $user_class->numrows($seat_number);
if($row_seat>0) {
$update_seat = $user_class->update($update_false);
$bol[$b] = FALSE;
} else {
if( $row_seat2>0 ) {
$insert_booking_dt = $user_class->insert($insert);
$update_seat = $user_class->update($update_true);
$bol[$b] = TRUE;
} else {
$bol[$b] = FALSE;
}
}
} else {
$insert_booking_dt = $user_class->insert($insert_without_seat);
$bol[$b] = TRUE;
}
if($bol[$b]) {
echo "FALSE";
header("location:../../../print.php?id=$id_booking");
}
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
}
}
}
Anything wrong with my php validation?
Because if I input array of $id_seat it will always redirect to print.php although validation is FALSE
for example if I input 3 array and then I echo FALSE WRONG FALSE FALSE
still redirect to print.php not to event.php
How can I read if one of array is get WRONG and then redirect to event.php?
How can I read if one of array is get WRONG and then redirect to event.php?
You may break out of for-loops.
Instead of:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
}
You could try:
else {
echo "WRONG";
header("location:../../../event.php?msg=Same seat number");
break 2;
}