Home > Software engineering >  Create custom array of input name values
Create custom array of input name values

Time:11-28

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?

CodePudding user response:

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

CodePudding user response:

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 )
  • Related