Jocker
Jocker

Reputation: 63

$_POST['formname'] issue

I have a small issue with $_POST. I have a Select with multiple options. In each option I have a value and another attribute, data, data I use to get 2 values out of the select.

<select id="theId" name="theName" onchange="changeSelect();">
    <option name="0" value="0.00" data="0.00"<?php if($val==0.00)echo 'selected=""';?>>0</option>
    <option name="1" value="10" data="15" <?php if($val==10)echo 'selected=""';?>>1</option>
    <option name="2" value="15" data="20" <?php if($val==15)echo 'selected=""';?>>2</option>
    <option name="3" value="20" data="30" <?php if($val==20)echo 'selected=""';?>>3</option>
</select>

I then use the following code to get the values:

var val1 = parseFloat($("select#theId option:selected").attr('value'));
var val2 = parseFloat($("select#theId option:selected").attr('data-us'));

Everything works perfectly... except when I try to store the values in the database... I can only use

$data = array(
        'val1' => $_POST['theName'],
        'val2' => $_POST['theName']
    );

as far as I know, since $_POST is using the form name ($_POST['formname']). If I use the above code, it will store the value of val1 for both val1 AND val2.

Is there any other way I could store these values?

Upvotes: 1

Views: 62

Answers (1)

Barmar
Barmar

Reputation: 781058

Add a hidden input to the form

<input type="hidden" name="theNameData" id="theNameData">

and copy the data to the hidden input.

$("#theNameData").val(val2);

Then you can use $_POST['theNameData'] in PHP to get this.

Upvotes: 2

Related Questions