-1

Below is code that I am using, when i submit the survey, arrrive and mode work but not trans/checkboxes, data comes back displayed as 'on' in table (survey) database (wdtlabwork), also the field is trans, same with the other two being mode n arrive in the database, all fields were type VARCHAR. Apologies if formatting is bad i am new to site. NOTE: I want the data to come back as either car, train or bus but because its checkboxes if the user checked train and car i want the database to display train and car.

HTML

<section id="content">
<form action="connect.php" method="post">
<div class="col-6 col-s-9">
  <h3>Available Transportation?</h3>
<input id="trans1" type="checkbox" name="trans[]"><label for ="trans1">Car</label>
<input id="trans2" type="checkbox" name="trans[]"><label for ="trans2">Train</label>
<input id="trans3" type="checkbox" name="trans[]"><label for ="trans3">Bus</label>
<h4>How do you intend to arrive at the Hostel?</h4>
<input id="arrive1" type="radio" value="car" name="arrive"><label for="arrive1">Car</label>
<input id="arrive2" type="radio" value="train" name="arrive"><label for="arrive2">Train</label>
<input id="arrive3" type="radio" value="bus" name="arrive"><label for="arrive3">Bus</label>
<h5>Preferred mode of transport?</h5>
<select name="mode">
<option selected hidden value="">Select Option</option>
<option value="car">Car</option>
<option value="train">Train</option>
<option value="bus">Bus</option>

</select><input type="submit" class="btn btn=primary"></div></section>

PHP

<?php
$arrive = $_POST['arrive'];
$trans = $_POST['trans'];
$mode = $_POST['mode'];

$conn = new mysqli('localhost', 'root','','wdtlabwork');


if ($conn->connect_error){
    die('Connection Failed : '.$conn-> connect_error);
}else{
$stmt = $conn->prepare("insert into survey(trans, arrive, mode)
    values(?, ?, ?)");
$stmt->bind_param("sss",$trans, $arrive, $mode);
$stmt->execute();
echo "registration successfully...";
$stmt->close();
$conn->close();
}

?>

  • as either car, train, bus separately or if the user checked car n train then both of them, depending on the user's input –  Oct 05 '19 at 18:10
  • 1
    On an unrelated note, you would benefit from enabling mysqli errors: [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Oct 05 '19 at 18:13
  • @Dharman 'Car' i changed them to the above example, still comes through as 'on' in database –  Oct 05 '19 at 18:20
  • @Dharman now the database comes up with 'Array' –  Oct 05 '19 at 18:45
  • Okay but i want to see the values in the database @Dharman –  Oct 05 '19 at 18:52
  • @Dharman It says in the description in the survey table, in the column called trans, for example if i selected car in trans, train in arrive and car in mode. The 3 columns, would come up array, train, car –  Oct 05 '19 at 18:55
  • No, thats if i select a single value, if i selected multiple for example car and train then the column would display on the table as car, train –  Oct 05 '19 at 18:59
  • the comma doesnt matter as long as it can store singular or multiple values it doesnt matter –  Oct 05 '19 at 19:01
  • @Dharman because its what they are asking me to do at uni and I havent been taught it, this is posted to find an answer not for you to question me why im doing it –  Oct 05 '19 at 19:04
  • @Dharman sorry i am just a bit stressed out, where in my php would i insert this sorry because i say this online somewhere but it didnt work where i added it –  Oct 05 '19 at 19:07
  • Kudos to your UNI for teaching you how to use prepared statements at least. At least that is one step in the right direction for the education system. :) – Dharman Oct 05 '19 at 19:08
  • @Dharman aw i learned from online not uni ahah but yeah where would i insert that line of code you sent me, the implode, where in my php –  Oct 05 '19 at 19:09

1 Answers1

1

Your input fields do not have value= attribute so they default to on as described here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Value

To get the values in the POST you need to specify the values, for example:

<input id="trans1" type="checkbox" name="trans[]" value ="car"><label for ="trans1">Car</label>
<input id="trans2" type="checkbox" name="trans[]" value ="train"><label for ="trans2">Train</label>
<input id="trans3" type="checkbox" name="trans[]" value ="bus"><label for ="trans3">Bus</label>

Then in your PHP you would receive an array of the selected elements in $_POST['trans']. If you would like to join them (not recommended) to be saved in a single field in the database you can just use implode(',', $_POST['trans']), for example:

$trans = implode(',', $_POST['trans']);
$arrive = $_POST['arrive'];
$mode = $_POST['mode'];
Dharman
  • 30,962
  • 25
  • 85
  • 135