irushdy
irushdy

Reputation: 1

a problrm when i turn a html form inputs into php array

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?

Upvotes: 0

Views: 29

Answers (1)

sic-sic
sic-sic

Reputation: 182

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];
?>

Upvotes: 1

Related Questions