0

This my code. How this multiselect values with array to post modelseardh.php

<p>
     <label>Country:</label>
     <select  multiple="multiple" name="country[]" class="qs_carlocation1                              carlocations" onChange="jsFunction()" id="selectOpt">
       <?php
            $sql_se = mysql_query('select * from `country`');
            while($se = mysql_fetch_assoc($sql_se)) {
           ?>
           <option value = "<?php echo $se['id'];?>"><?php echo $se['country'];</option>
          <?php
            }
           ?>                       
           </select>
        </p>    `         
          <p id="change_form">
             <label>Mark:</label>
              <select  multiple="multiple"name="search_mark[]"class="qs_carlocation1 carlocations" >
            <?php
               $sql_search_mark = mysql_query('select * from `mark`');
               while($smark = mysql_fetch_assoc($sql_search_mark)) {
             ?>
         <option value="<?php echo $smark['id'];?>"><?php echo $smark['name'];?></option>
            <?php
               }
             ?>
            <script>
              function jsFunction(){
              var select = document.getElementById("selectOpt").value;
               $.post( 
                "modelsearch.php",
                {
                  id: "" + select + ""
                },
                 function(data) 
                 {
                   $('#change_form').html(data);
                 }
                );
              }
                </script>                           
              </select>
             </p>       

             <?PHP
                //this modelsearch.php
               include('../db.php');
                $id = $_POST['id'];
               ?>
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38

2 Answers2

0

There's wrong with enclosing & misplaced tag in the following line that possibly make the rendering ouput fails:

 <option value = "<?php echo $se['id'];?>"><?php echo $se['country'];"></option>

which is missing ?>"> right before the </option>, and you need to remove "> between ?> <?php it should be :

<option value = "<?php echo $se['id'];?><?php echo $se['country'];?>"></option>

and you also don't ouput any values between <option> xxx </ouput>, so the value attribute is assigned in the <option> but nothing to be ouput. I meant that xxx to be output

and FYI , you dont need to multiple echo the values, just concatenate the values :

 <option value = "<?php echo $se['id'] +""+ $se['country'];?>" ></option>
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19
  • As I understood, OP asks how to handle data from multiple select field, not how to generate it. – chapay Jan 27 '15 at 08:24
  • that's what i was considering before answering, maybe he wants that, but he can't do anything if his value attribute never get assigned correctly, thats why when he clicks the list, nothing happens, but i'm not sure – Fevly Pallar Jan 27 '15 at 08:28
0

First of all, you don't need to keep the country name in Array change country[] to country only

<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script language='javascript'>
function showselection() 
 var frm = document.testingform //this is the name of the form can be as your form name
var opt = frm.country  //this is the multiselect option name
var numofoptions = opt.length
    var selValue = new Array  
 var j = 0 
for (i=0; i<numofoptions; i++)  
  {  
if (opt[i].selected === true) 
{  
 selValue[j] = opt[i].value  
j++
}
  }
var myvalues=document.getElementById('yourhiddenvaluename').value=selValue;
 $.post( 'modelsearch.php',{ newva: myvalues }, function( data ) {
$('#change_form').html(data)
});
}  
</script>

<select name='country' multiple onchange='showselection()'> 
//Your values
</select> 
 <input type="text" id="yourhiddenvaluename" name="seletedvalued" />

In modelsearch.php:

print_r($_POST['newva']);
Jad
  • 479
  • 2
  • 10
  • 23
Ramesh Singh
  • 98
  • 1
  • 14