how do i extract values from an array to show on a html page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rotaryfreak
    New Member
    • Oct 2008
    • 74

    how do i extract values from an array to show on a html page

    hello,

    im trying to build an application that will incorporate a "forgot password" feature. in order for my feature to work correctly, i am asking the user to type in his/her email address and he/she will have to answer a security question.

    Code:
    <form id="form1" name="form1" method="get" action="emailRetrieve.php" >
        	<label>Please enter your email address:</label><br/>
      		<label> Email address: <input type="text" name="emailText" id="emailText" size="50" /><br/><br/>
    			  
                  Secret Question: <br/>
                  Secret Answer: <input type="text" name="secretAnswer" id="secretAnswer" size="49" /><br/>
           		
    		</label>
            <input type="submit" value="Submit"/>
    		</label>
    	</form>
    in my php script, i created an array with only 3 questions. Every time the page is refreshed, or other users use the "forgot password" feature, i am implementing a random function which chooses a random index of the array and will hence display a different question every time.

    my problem is, when i hardcode the php script into the html page right after the "Secret Question" it works perfectly fine and i see the questions randomly changing when the page is refreshed. The problem now is that i need to send that same question for verification in my database. I can not access this variable outside the .html page. Now, when i move the php block to another file, i can not randomly generate my question and insert it into the html page. i know i have to use the $_POST method but whatever i try is not working

    can anyone shed some light on this? how to i use php to randomly generate an index in my array and then insert it into my html page?

    the code that im using for my 3 random questions is:

    Code:
     <?php
    
    $securQueArr = array("1" => "what was your first car", "2" => "what is your mothers name", "3" => "which city were you born in");
    $randInt = rand(1,3);
    
    
    $securityQuestion = $securQueArr[$randInt];
    
    ?> 
    
    any ideas?
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    In your form you need something like:
    Code:
    <p>Secret Question: <?php echo $securityQuestion; ?></p>
    To display the question.

    You will also need to send the $securityQuesti on through to your processing script which can either be done with $_SESSION's or $_POST. I am going to recommend $_POST (which is less secure) because all you're doing is sending the question. To do that, I suggest having a hidden input with the question number as the value:
    Code:
    <input type="hidden" name="Q_Num" value="<?php echo $randInt; ?>" />
    Then in your processing script you can get the question number used like:
    Code:
    // Validation of $_POST variables
    $Q_Num = $_POST['Q_Num'];

    Comment

    • rotaryfreak
      New Member
      • Oct 2008
      • 74

      #3
      it seems to be almost working :| i get an error:
      Notice: Undefined variable: securityQuestio n in C:\Program Files\Wamp Server\www\Movi e Database\EmailP wd.php on line 20

      and when i look at line 20, it's this line:

      Secret Question: <?php echo $securityQuesti on; ?>

      strange because i do define the value in my script...but, at least now im getting some input after Security Question

      any ideas?

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Are you defining it above that line? Is the case all the same? If yes to both of those, post your page code and let me have a look.

        Comment

        • rotaryfreak
          New Member
          • Oct 2008
          • 74

          #5
          :) worked perfectly. here's what i did. After you asked if it was defined, i included the entire php script above the Secret Question line, and modified my emailRetrieve.p hp $Q_Num = $_POST['Q_Num']; to $Q_Num = $_GET['Q_Num'];
          now my question changed randomly AND i have access to it through my emailRetrieve.p hp script so i can validate it against the entry in my database. Everything works perfectly, thank you so much!

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            No problem, glad it works.

            Didn't notice your form method was GET, but, if you're happy with that, then great ;)

            Comment

            Working...