This question already has an answer here:
I am using an order form on my website and the client has multiple option of what type of graphics that they require but I'm not sure how I read in the value they selected for the multiple choice option.
Here is the .html code for the order form:
<section id="Order">
<div class="container">
<h2>Order</h2>
<form action="order.php" method="post" id="usrform">
<fieldset>
<legend>What type of graphics would you like to purchase? </legend>
<p>
<input type="radio" name="size" id="item_1" value="Social_Media">
<label for="item_1">Social Media</label>
</p>
<p>
<input type="radio" name="size" id="item_2" value="Twitch_Package">
<label for="item_2">Twitch Package</label>
</p>
<p>
<input type="radio" name="size" id="item_3" value="Mascot_Logo">
<label for="item_3">Macot Logo</label>
</p>
<p>
<input type="radio" name="size" id="item_4" value="2D_Logo">
<label for="item_3">2D Logo</label>
</p>
<p>
<input type="radio" name="size" id="item_5" value="3D_logo">
<label for="item_3">3D Logo</label>
</p>
<p>
<input type="radio" name="size" id="item_6" value="other">
<label for="item_3">Other</label>
</p>
</fieldset> </br> </br>
Enter email: </br>
<input type = "email" name = "userEmail" autocomplete="on" required> </br> </br>
Enter details </br>
<textarea rows="4" cols="50" name="comment" form="usrform"></textarea> </br> </br>
<input type = "submit" name = "submit" value = "SUBMIT" >
</form>
</div>
</section>
This is the .php code below from the order.php page:
<?php
$host="localhost";
$user="";
$password="";
$dbname="id321208_clients";
$cxn=mysqli_connect($host,$user,$password,$dbname) or die("Couldn't connect to the server");
$type=$_POST['size'];
$email=$_POST['userEmail'];
$details=$_POST['comment'];
$query="INSERT INTO usersDetails (size, userEmail, comment) VALUES ('$type', '$email', '$details')";
if(mysqli_query($cxn,$query)){
echo"I have recieved your email! I will contact you as soon as I can.";
}else{
echo"Error- ".mysqli_error($cxn);
}
echo '<a href="Order.html"> Click to return</a>';
mysqli_close($cxn);
?>
</div>
first of all, your radio button is overwritten each time you declared it, since you are using the same name, please set them using this code:
<section id="Order">
<div class="container">
<h2>Order</h2>
<form action="order.php" method="post" id="usrform">
<fieldset>
<legend>What type of graphics would you like to purchase? </legend>
<p>
<input type="checkbox" name="size[]" id="item_1" value="Social_Media">
<label for="item_1">Social Media</label>
</p>
<p>
<input type="checkbox" name="size[]" id="item_2" value="Twitch_Package">
<label for="item_2">Twitch Package</label>
</p>
<p>
<input type="checkbox" name="size[]" id="item_3" value="Mascot_Logo">
<label for="item_3">Macot Logo</label>
</p>
<p>
<input type="checkbox" name="size[]" id="item_4" value="2D_Logo">
<label for="item_3">2D Logo</label>
</p>
<p>
<input type="checkbox" name="size[]" id="item_5" value="3D_logo">
<label for="item_3">3D Logo</label>
</p>
<p>
<input type="checkbox" name="size[]" id="item_6" value="other">
<label for="item_3">Other</label>
</p>
</fieldset> </br> </br>
Enter email: </br>
<input type = "email" name = "userEmail" autocomplete="on" required> </br> </br>
Enter details </br>
<textarea rows="4" cols="50" name="comment" form="usrform"></textarea> </br> </br>
<input type = "submit" name = "submit" value = "SUBMIT" >
</form>
</div>
</section>
After that, you can access them as array using:
$type=$_POST['size'];
foreach($type as $selectedType){
echo $selectedType."<br/>";
}
You can do whatever you need now with them.