Get data from a textbox and compare it with a line in a file

Links for php scripts

Moderators: macek, egami, gesf

bitakid1997
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Jun 21, 2014 9:37 pm

Get data from a textbox and compare it with a line in a file

Postby bitakid1997 » Sat Jun 21, 2014 9:50 pm

Hi, I am a newbie and i start to learn PHP Script for a few months! I'm from Vietnamese so my English is very bad!
I have a question: I want to get data from a textbox and compare it with a line data in a text file. If it 's same, a message box appear with report: "You sign in success." and if it 's not same, a message box appear with: "You don't have an account. Please register a n account.". Done!
Please help me. Thanks a lot!

kladrian
New php-forum User
New php-forum User
Posts: 12
Joined: Thu Aug 14, 2014 1:13 am
Contact:

Re: Get data from a textbox and compare it with a line in a

Postby kladrian » Thu Aug 14, 2014 6:37 am

Hi,

first create a form with a text box.

In your action php you should get the value of the text box by searching it into the $_POST (or $_REQUEST) array.

Code: Select all

$text_val = $_POST['text_box_name'];


then you'll have to search it in the file; there are few ways to do that.
The ugliest and yet quickest way is

Code: Select all

$file_content = file_get_contents('/absolute/path/to/file');
if (strpos($file_content, $text_val) !== FALSE) {
  // OK
  echo "You sign in success.";
}
else {
  // KO
  echo "You don't have an account. Please register a n account.";
}


---
kladrian

User avatar
freshnet
php-forum Active User
php-forum Active User
Posts: 285
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Get data from a textbox and compare it with a line in a

Postby freshnet » Sat Oct 18, 2014 4:53 pm

You probably don't want to do it this way. For example, if the string you're matching is 'hello my name is bob', then none of the following will work:

' hello my name is bob' (space at the beginning)
'hello my name is bob!' (character at the end)
'hello my name is Bob' (capitalized letter)

You probably want to do at least one of the following:

  1. Trim the textbox text using trim() to remove spaces at the beginning and end and use stripos to make it case insensitive (easiest option)
  2. Use preg_match http://php.net/manual/en/function.preg-match.php to check the entire string for matches. You can also make it case insensitive here if you like (although stripos() can do that too)


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest