Select box sends wrong value to db

Hello, since i made a select box that if an option is selected a subcategory select box appears, I got problems with sending it to the db.

html code for the select boxes:

<div class="form-group">
	<label>Kost categorie</label>
	<select name="category" class="mainselect">
		<option value="">Selecteer</option>
		<option value="woning">Woning</option>
		<option value="vervoer">Vervoer</option>
	</select>


	<select name="subcategory" class="subselect">
		<optgroup label="woning">
			<option value="huishouden">Huishouden</option>
			<option value="huur">Huur</option>
		</optgroup>
	</select>

	<select name="subcategory" class="subselect">
		<optgroup label="Vervoer">
			<option value="auto">Auto</option>
			<option value="fiets">Fiets</option>
		</optgroup>
	</select>
</div>

the jquery script to show the second select box:

var $sub = $('select.subselect');

$('select').first().change(function() {    
    $sub.hide();
    // If the first option is not selected
    if (this.selectedIndex > 0)
       $sub.eq(this.selectedIndex - 1).show();
});

and then the insert into db code:

$costname = $_POST['costname'];
$category = $_POST['category'];
$subcategory = $_POST['subcategory'];
$price = $_POST['price'];
$info = $_POST['info'];
$costdate = $_POST['costdate'];
$iduser = $_SESSION['id'];

//Insert into db

if($stmt = $conn->prepare("INSERT INTO costs (costname, category, subcategory, price, info, userid, costdate)
  VALUES (?, ?, ?, ?, ?, ?, ?)")) {
    $stmt->bind_param("sssdsis", $costname, $category, $subcategory, $price, $info, $iduser, $costdate);
    $stmt->execute();
  }

Somehow if i add the values everything is ok even the main category is fine but the subcategory is always the first value of the last select box in this case: auto.

I hope someone can help me i’ve been stuck on this for a day now :frowning:

At a first look i think that you must add a submit button to send all of these.I mean after you select the values of checkboxes then you must add an event that send all of these into database

<select name="subcategory" class="subselect">
 ⋮ 
<select name="subcategory" class="subselect">

To Tell the Truth, Will the real subcategory please stand up.

1 Like

I’m sorry i should have explained a bit better.
The submit button is there and it sends all the data it just sends 1 wrong string to the column subcategory which is selected when the the main category is selected.
But it always picks the first option of the last subcategory

there are dozens of subcategories how can i change the names if my code works like this:

$costname = $_POST['costname'];
$category = $_POST['category'];
$subcategory = $_POST['subcategory'];
$price = $_POST['price'];
$info = $_POST['info'];
$costdate = $_POST['costdate'];
$iduser = $_SESSION['id'];

any idea’s?

You have 2 select tags with the same name “subcategory”.Give the second

2 Likes

how is that gonna make a difference? i still have to put 1 variable into the database:

if($stmt = $conn->prepare("INSERT INTO costs (costname, category, subcategory, price, info, userid, costdate)
  VALUES (?, ?, ?, ?, ?, ?, ?)")) {
    $stmt->bind_param("sssdsis", $costname, $category, $subcategory, $price, $info, $iduser, $costdate);
    $stmt->execute();
  }

Then you want only one select with all the options in it?

<select name="subcategory" class="subselect">
	<optgroup label="woning">
		<option value="huishouden">Huishouden</option>
		<option value="huur">Huur</option>
	</optgroup>
	<optgroup label="Vervoer">
		<option value="auto">Auto</option>
		<option value="fiets">Fiets</option>
	</optgroup>
</select>

then users get all of the options when they select a main category…
The idea is that if they pick a main categorie only the options for that categorie are showing.

The problem seems to me (someone with virtually zero experience of JQuery and JS, I should add) that you’ve got two HTML option tags with the same name, and you’ve got some JS code that controls which one of them is displayed. You don’t control which of them is submitted as your form data, only which one is shown on the screen. Would hiding one of the select fields remove it from the form submission, or would it just come through like any other hidden variable?

At the start of your form processing code, what do you get if you var_dump($_POST) for each of the options?

1 Like

Then you modify the script to show only the correct optgroup.
The problem has already been pointed out a number of times.

To be clear, you can’t have two separate inputs with the same name, one will overwrite the other. The back-end code is oblivious to the front-end code that hides things, it does not know which of the two to use, so uses the last, which has overwritten the first.

Your options are:-

or

would it work if i check every subcategory with an if statement and then tie it to the variable $subcategory which i use in the query?

like this should work then?

if (isset($_POST['subcategory_1'])) {
	$subcategory = $_POST['subcategory_1'];
}
if (isset($_POST['subcategory_2'])) {
	$subcategory = $_POST['subcategory_1'];
}

I added this in my script and changed the select boxes names to subcategory_1 and 2 but still it keeps selecting the last select box to input into db…

if i select a different value from the second subcategory it does change the value tho…

Typo?

if (isset($_POST['subcategory_2'])) {
	$subcategory = $_POST['subcategory_1'];   // surely this should end in '2'?
}

I’m not sure what the behaviour of both select tags being submitted will be - won’t they both get submitted, and therefore respond ‘true’ to your isset() test? Perhaps check the value of the main category selection to decide which subcategory to use.

1 Like

Both will always be set regardless of whether they were manually selected.
I thought the choice between subcategories was based on the previous selection, if so then your conditions should reflect that.

if($_POST['category'] == 'something`) {
   $subcategory = $_POST['subcategory_1'];
}
elseif(...) {...}
2 Likes

Ty very much that fixed it!
Thanks for the help and well explained advice you guys rock! :slight_smile:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.