1

here is my code that generates random characters

How can i get this random characters when i click my button 'submit' . and get that as a variable to save on database. please help

this is my button

<span class="input-group-btn">
     <button class="btn btn-info"  type="submit" name="submit">POST</button>
</span>

this generates random characters

<?php
$result = "";

$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$chararray = str_split($chars);
for($i = 0; $i < 7 ; $i++){
    $randitem = array_rand($chararray);
    $result .= "".$chararray[$randitem];
}

echo $result;
?>

i want to get that random character as a variable like $post_randomid = $_POST[random_id];

2
  • where is your form Commented Jan 2, 2017 at 15:35
  • here just help me to get that random id when i click that button Commented Jan 2, 2017 at 15:41

3 Answers 3

1
<?php
if(isset($_POST['submit'])) {
    if(isset($_POST['rand'])) {
        echo $_POST['rand'];
    }
}
?>
<form method="POST" >
    //this generates random characters
    <?php
    $result = "";
    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $chararray = str_split($chars);
    for($i = 0; $i < 7 ; $i++){
    $randitem = array_rand($chararray);
    $result .= "".$chararray[$randitem];
    }
    echo $result;
    // i want to get that random character as a variable like  $post_randomid =$_POST[random_id];
    ?>
    <input type="hidden" value="<?php echo $result;?>" name="rand" />
    //this is my button
    <span class="input-group-btn">
        <button class="btn btn-info"  type="submit" name="submit">POST</button>
    </span>
</form>   
Sign up to request clarification or add additional context in comments.

3 Comments

thanks arun it works but can you help me to insert in database ?
how can i insert that $result value in databese $insert ="insert into table (rand_id) values ('$result' ) ";
my reputation is not 15 yet so i am unable to vote.... i will as soon as i reach their
0

Send the value either in the session on the server or create a hidden input type on the client. The second option solves your question.

Comments

0
    <?php
    $result = "";

    $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    $chararray = str_split($chars);
    for($i = 0; $i < 7 ; $i++){
            $randitem = array_rand($chararray);
            $result .= "".$chararray[$randitem];
    }
    ?>

    <form action="my_php_database_script.php" method="POST">
            <input name="chars" type="hidden" value="<?php echo $result; ?>" />
            <button type="submit" name="submit">POST</button>
    </form>

You could access your result with $_POST["chars"] in your script php (my_php_database_script.php) to save it in database.

If you want to do it asynchronously without leaving this page, you have to deal with AJAX.

Comments

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.