/    Sign up×
Community /Pin to ProfileBookmark

Form validation in word game

I have started writing a word game in PHP that displays a number of text boxes equal to the number of letters in a mystery word. As the user types in a letter in the box the entry is checked against the hidden word and if correct the box should turn green. I have written the following code so far which draws the boxes but I am stumped on how to check the input into a box. I do not want to send the form back every time a letter is entered so I need to use client side coding – I am guessing that JavaScript would be the answer hence posting in this forum not PHP.

Code so far:
<?php
// Get word from dictionary
$newword = “nuisance”;
$definition = “an annoying person or thing”;
?>
<div class=”jumbotron”>
<h1>SpellBound</h2>
<p>Below you will see a definition of one of the words in the dictionary. See if you can spell the word by typing the letters in the empty boxes below</p>
</div>

<div class=”panel panel-info”>
<div class=”panel-heading”>How do you spell the word that means</div>
<div class=”panel-body”><?php echo $definition;?></div>
</div>

<?php
$wordlen = strlen($newword);

<form>
<?php for ($i = 1; $i <= $wordlen; $i++) {
echo ‘<input type=”text” size=”1″ maxlength=”1″ id=”letter-‘ . $i . ‘” >’;
}
?>
</form>

Any suggestions ?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rootMar 02.2016 — Yes...

  • 1. Wrap code blocks in forum tags, it helps people help you! (see my signature)


  • 2...... Are you JavaScript or JQuery? If you are JQuery then I suggest you ask again in the correct forum (see my signature)

    I suggest that you have a change to your script above, use the form correctly and use a combination of JavaScript / AJAX and PHP backend script that is your assistant to AJAX

    [code=php]
    <?php
    $wordId = 1234; // would be the record ID of the word you are pulling from the database
    ?>
    <form name="word" action="javascript:;" method="post" enctype="multipart/form-data" onsubmit="ajax_form_process(this);">
    <input name="wordId" type="hidden" value="<?php echo $wordId;?>">
    <?php
    for ($i = 1; $i <= $wordlen; $i++) {
    echo sprintf(" <input name='letters' type='text' size='1' maxlength='1' tabindex='%s' >",(1000+$i) );
    }
    ?>
    </form>[/code]


    the input names letters will be an array, array position (starting at 0) denotes the letter position in the submitted form via AJAX...

    the function you write for AJAX, I call it ajax_form_process(this); passes the form contents via the this object, makes forms simpler to pass the data to the ajax form processor and your submitting a form and thats the bit along with the PHP backend that helps validate the letters that you need to work out ?
    ×

    Success!

    Help @kmcquaid spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.6,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...