skip to Main Content

If I have two inputs like the following

<input type="text" name="id">
<input type="text" name="name">

Then it is possible to get values in array on backend like this
array("id"=>"name")

If it is possible then how it can be done?

2

Answers


  1. You can send your post via POST method.

    <form name="form" action="" method="post">
    <input type="text" name="id">
    <input type="text" name="name">
    <input type="submit" name="submit_button" 
               value="Send"/>
    </form>
    

    After that :

    <?php
    $array = [$_POST['id'] => $_POST['name']];
    
    Login or Signup to reply.
  2. Hope you are doing well and good.

    Umm, I just got your query. You want to make id as a key and name as a value in array. So here, May be i found your solution.

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <title>Document</title>
    </head>
    
    <body>
        <form method="post">
            <input type="text" name="id[]">
            <input type="text" name="name[]">
            <input type="submit" name="submit" value="submit">
        </form>
    </body>
    
    </html>
    
    <?php
        if(isset($_POST['submit']))
        {
            $combineArr = array_combine($_POST['id'], $_POST['name']);
            print_r($combineArr);
        }
    ?>
    

    From the above code you can get answer like below,

    Array ( [id] => name )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search