-4

I get the error because of my select form. How i can take the select form into my php file ?

Here is my code:

<select name='md' multiple="yes" size="10">
    <optgroup label="Оpel">
        <option value="Astra" name='Ast'>Astra</option>
    </optgroup>
    <optgroup label="Nissan">        
        <option value="Pulsar" name='Puls'>Pulsar</option>
        <option value="Almera" name='Alm'>Almera</option>
    </optgroup>
    <optgroup label="Honda">
        <option value="Civic" name='Civ'>Civic</option>
    </optgroup>     
    <optgroup label="Audi">
        <option value="Q7" name='Q'>Q7</option>                 
    </optgroup>

    <optgroup label="BMW">
        <option value="M3" name='M'>M3</option>
    </optgroup>
    <optgroup label="Renault">
        <option value="Megane" name='Meg'>Megane</option>
    </optgroup>
    <optgroup label="Mercedes">
        <option value="G-Class" name='G'>G-Class</option>
    </optgroup>
</select></br></br>

Цвят :
<select name='col' size='5'>
    <option value='white' name='wh'>Бял</option>
    <option value='black' name='bl'>Черен</option>
    <option value='yellow' name='ye'>Жълт</option>
    <option value='brown' name='br'>Кафяв</option>
    <option value='grey' name='gr'>Сив</option>
</select></br></br>

And my php code:

 $md = $_POST['md'];
 $col = $_POST['col'];

Somewhere i saw that i should use ajax do i ? Is it necessary ?

Bjoern
  • 15,934
  • 4
  • 43
  • 48
Anton Papazov
  • 85
  • 1
  • 2
  • 9

3 Answers3

0

try with

if(isset($_POST['md'])){
  $md = $_POST['md'];
}else{
  $md = '';
} 

if(isset($_POST['col'])){
 $col = $_POST['col'];
}else{
  $col = '';
}

or

$md = (isset($_POST['md'])) ? $_POST['md'] : ''; 
$col = (isset($_POST['col'])) ? $_POST['col'] : '';
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
  • Have you just shamelessly copied the ternary operator from my answer? – Tibor B. Dec 04 '14 at 12:50
  • Oh, and even the `else {` part in your main answer... great. – Tibor B. Dec 04 '14 at 12:50
  • Just look into the answer time mine answer was before your answer, I don't know you copied my answer or I copied your answer. You should be shameless while copying the answer of other people – Ram Sharma Dec 04 '14 at 13:16
0

Your PHP code shows "Notice: Undefined index: ...", because you don't have anything selected in the select boxes, when you initially open the file. To prevent it, use if (isset(...)):

if (isset($_POST['md']) {
  $md = $_POST['md'];
} else {
  $md = '';
}
if (isset($_POST['col']) {
  $col = $_POST['col'];
} else {
  $col = '';
}

Or you could use ternary operators as a shorter solution:

$md = (isset($_POST['md'])) ? $_POST['md'] : '';
$col = (isset($_POST['col'])) ? $_POST['col'] : '';
Tibor B.
  • 1,680
  • 1
  • 10
  • 12
0

because i think you are receiving array variable for multiple selection. use array to store data.like this

    <label for="aoi">Area of Interest:</label>
      <select id="sel_aoi" name="aoi[]" multiple="multiple">
        <option value="hr-executive">HR Executives</option>
        <option value="sr-manager">Sr. Manager</option>
        <option value="service-advisor">Service Advisor</option>
        <option value="production">Production Engineer</option>
        <option value="mechanical">Mechanical Engineer</option>
      </select>


    $aoi = implode(',', $_POST['aoi']);