Skip to content
Advertisement

Problems with php validations not working

I was tasked on making simple i-Prepaid Reload programming.So far, I have problem with validations not working on reload.php page. As if there were no validations set at all and it went to result.php after pressing Buy button. I couldn’t figure it out what’s the cause of this error. There should be some errors appear is I put alphabets or less than 10 numbers or ignore all of those options in reload.php.

login.php

JavaScript

reload.php

JavaScript

‘result.php’

JavaScript

Advertisement

Answer

Please note that array_search, if fails to find any record, will return false in normal circumstances.

However, array_search may return Boolean false, but may also return a non-Boolean value which evaluates to false (See official documentation: https://www.php.net/manual/en/function.array-search.php)

On the other hand, if the match is on the 1st record, the return result will be 0 (1st record matches).

Hence, to cover all the cases, you may change your login.php to :

login.php

JavaScript

For reload.php, please

  1. amend your original code so that the submit it to itself (otherwise the validation will never happen)
  2. If all data are validated to be correct, store the submitted data as session variable (namely $_SESSION[‘phonenumber’], $_SESSION[‘Telco’] and $_SESSION[‘ReloadCredit’]), then redirect to result.php

reload.php

JavaScript

For result.php, just display the submitted data thru the session variables:

result.php

JavaScript

However, in future, please consider using a database approach to manage the users’ credentials. Otherwise you need to update the PHP array list everytime you have new / changed username and/or passwords.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement