How To Distinguish Between Words In A Text Box?

Discussion in 'PHP' started by JimJamYaha, Mar 22, 2009.

  1. #1
    Hi All,

    I've got a webpage with a text box in, which the user can type text into (obviously!).

    Im trying to figure out how to make it so that if a user types the word "cat" then in another text box somewhere else on the page the word "dog" appears.

    Does anybody know how to do this? It seems pretty difficult because there needs to be a way to distinguish between words in a text box.

    Your help is much appreciated!

    Thanks!
     
    JimJamYaha, Mar 22, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you want it to happen in real time, as the person is typing? If so, this looks like a JavaScript question, not a PHP question.

    Otherwise, if you're willing or able to wait until the form is submitted:
    $words = preg_split('/\W+/', strtolower($_REQUEST['cat_field']));
    if (in_array('cat', $words))
       $dog_field = "dog";
    Code (markup):
     
    SmallPotatoes, Mar 23, 2009 IP
  3. JimJamYaha

    JimJamYaha Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks small potatoes.

    Yes, ideally I wanted it realtime - oh well, looks like Im going to have to learn Javascript now!
     
    JimJamYaha, Mar 23, 2009 IP
  4. omglol

    omglol Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try a javascript onchange callback using grep and a hidden div.
     
    omglol, Mar 23, 2009 IP
  5. kenfrank

    kenfrank Peon

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Check out jQuery and its AJAX support, it'll make that part a lot easier :)
     
    kenfrank, Mar 24, 2009 IP