0

I have an issue.. I created a php script that generates random number: the script goes thus:

</head>
<div id="inner-content">
<form action="" method="post" name="form1">
<fieldset>
<legend>Please click on the button to generate a random number</legend>
<input type="submit" value="Generate" /><br />
<?php
$length = 10;
$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX‌​YZ"), 0, $length);
echo "$randomString";
?>
<p>
<input type="button" name="button1" value="Save" />
</div>
</fieldset>
<body>
</body>
</html>

Now i have have two serious worries: 1) How do i trap the generated number and save it into a database owing to the fact that the submit button is the one generating the random number. I want a situation whereby when we click save, the number goes into the database.

2) I have two tables,(table A and table B, table A saves all the information being entered and submitted by the user, table B has the generated codes, table A will love to validate a particular field from the form and validate the code in table B, then if codes matches, the user can submit, else the user gets an error and asked to fill in the correct code which resides in table B.also, i have the controller from table A which saves the form. here's the form.

5
  • Fun to read, but I seem to completely miss out on the meaning... 3 times in a row. Commented Jan 3, 2014 at 15:20
  • @LexPodgorny Podogorny please read again sir.. Commented Jan 3, 2014 at 15:23
  • here is the php random script: </head> <div id="inner-content"> <form action="" method="post" name="form1"> <fieldset> <legend>Please click on the button to generate a random number</legend> <input type="submit" value="Generate" /> <br /> <?php $length = 10; $randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length); echo "$randomString"; ?> <p><input type="button" name="button1" value="Save" /> </div> </fieldset> <body> </body> </html> Commented Jan 3, 2014 at 15:23
  • Submit button does not generate your random code. It posts into the same page ant then you see new generated code. By the way, your html is not valid. Commented Jan 3, 2014 at 15:30
  • hello @Paul, can you kindly help me with the right html?? Thanks Commented Jan 3, 2014 at 15:34

1 Answer 1

1

The problem here is, like the comments on your post pointed it out, that your code is executed all the way before being sent to the browser. It implies that your random string (and not number with what I see) is generated and shown directly before the client has clicked on any button.

To solve this problem, you have to do this in 2 steps : first, if the user hasn't entered anything yet, show the form. Then, if the user has already submitted the form (making it reload the page), you can generate your random number and show it.

Here is how it can be solved (I did not include HTML headers, just relevant code) :

<?php

//Checking if the user has already validated the form we show the string
if(isset($_POST["submit"]))
{
    $length = 10;
    $randomString =  substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX‌​YZ"), 0, $length);
    echo "$randomString";
}
else //If he hadn't show the form
{
    ?>
    <form method="post" action="">
        <input type="submit" name="submit" value="Generate string" />
    </form>
    <?php
}
?>

Then you can save it in your database (you can make it optional for example by adding a checkbox in your form and checking after your generation if it is checked, then saving into database if it is).

Sign up to request clarification or add additional context in comments.

1 Comment

Good job @linkboss, i have also tried that method, but my worries is how to trap the generated code and send it into the database..i think we are almost getting there !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.