0

I'm trying to write a php script that takes the input from this form and creates an array with a unique key for each input box/text boxes content.

<div class="container">
  <div class="row">
    <div class="col-3 mx-auto">
      <form action="adminhandler.php">
        <div class="form-group">
          <label for="product name">Enter product name</label>
          <input type="text" class="form-control" id="productName" aria-describedby="emailHelp" placeholder="" name="productname">
        </div>
        <div class="form-group">
          <label for="product name">Select product category</label>
          <select name="select" class=" custom-select">
          <option selected>Open this select menu</option>
          <option value="1">Formal Shoes</option>
          <option value="2">Sneakers</option>
          <option value="3">Tracksuits</option>
          <option value="3">Clothing</option>
          <option value="3">Accessories</option>
        </select>
        </div>
        <div class="form-group">
          <label for="product price">Enter product price(Naira)</label>
          <input type="text" class="form-control" id="productPrice" aria-describedby="emailHelp" placeholder="" name="price">
        </div>
        <div class="form-group">
          <label for="product name">Select image</label>
          <input type="file" class="form-control-file" id="productimage" aria-describedby="emailHelp" placeholder="">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    </div>
  </div>
</div>
5
  • That's basically exactly what $_POST and $_GET are. Commented Nov 30, 2018 at 20:49
  • I know what they are, for some reason i've been getting errors trying to use them so i wanted to see someone else's code to figure out what i'm doing wrong Commented Nov 30, 2018 at 20:57
  • give every input field a name and you will be able to read it in php with $_GET or $_POST based on what form method was used. Commented Nov 30, 2018 at 20:57
  • 4
    " i've been getting errors" Show your code (in the question) and also show the exact error message. Commented Nov 30, 2018 at 20:58
  • Remove some unneeded sentences. Commented Dec 1, 2018 at 6:56

1 Answer 1

1

I think you want to use a form as an array and then pass the same data as an array.If you are using the $_POST, you will need to use the name attribute of HTML. And use something like this :

<input name="person[1][first_name]" value="jane" />

This when passing through PHP will be sent as Array. Here is the reference : http://php.net/manual/en/reserved.variables.post.php An Excerpt from one of the contributors :

   <form>
<input name="person[0][first_name]" value="john" />
<input name="person[0][last_name]" value="smith" />

<input name="person[1][first_name]" value="jane" />
<input name="person[1][last_name]" value="jones" />
</form>

When Trying To Process Values using PHP

<?php
var_dump($_POST['person']);
//will get you something like:
array (
0 => array('first_name'=>'john','last_name'=>'smith'),
1 => array('first_name'=>'jane','last_name'=>'jones'),
)
?>
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.