0

My html file contains the following form

<form action="action.php" method="get">
    First name:<br>
    <input type="text" name="name[]" /><br>
    Second name:<br>
    <input type="text" name="name[]" /><br>
    Third name:<br>
    <input type="text" name="name[]" /><br>
    Forth name:<br>
    <input type="text" name="name[]" /><br>
    <input type="submit" value="submit">
</form>

and I want the output to be one random value of the inputs , so my action.php looks like

<?php

    $output = $_POST['name'];

    $key = array_rand($output);
    echo $output[$key];

?>

but this doesn't work and gives me the following

Notice: Undefined index: name in C:\xampp\htdocs\myfiles\action.php on line 8

Warning: array_rand() expects parameter 1 to be array, null given in C:\xampp\htdocs\myfiles\action.php on line 10

can any one help please?

4
  • 1
    your method is "GET" why you use "$_POST" ? Commented May 28, 2017 at 20:41
  • change the form method to post: method="post" Commented May 28, 2017 at 20:43
  • @RyanAW you are right, my bad, thank you Commented May 28, 2017 at 20:55
  • @JosanIracheta thank you Commented May 28, 2017 at 20:55

1 Answer 1

1

the form is submitted using get method

so you should use $_GET to retrieve sent datas like this:

<?php
     $output = $_GET['name'];
     $key = array_rand($output);
     echo $output[$key];
?>
Sign up to request clarification or add additional context in comments.

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.