简体   繁体   中英

How to pass loop value from hidden input field within select form to Ajax

I thought this would be simple?

I have a select form with a hidden input field to carry a looped ID. I wish to pass both the selected option VALUE and the hidden input field VALUE to Ajax Function.

When I place value='$id_static[$k]' as an option in the select field the Id loops properly.

when I pass as value in the hidden input it only passes '1' regardless of row selected.

See condensed code below.

  <form>
       <div class='select-style'>
  <select  onChange='update(this.value)' id='gcc' name='gcc' class='form-control'>                        
        <option value='i2000'>i 2000</option>
        <option value='$id_static[$k]'>i 3000</option>
        <option value='i4000'>i 4000</option>
  </select>  
      <input  type='hidden'  id='g' name='g' value='$id_static[$k]'>
  </div>           
  </form>

Notice how i have $id_static[$k] as value in one of my Select options. I used as testing only... Ajax processes loop perfectly. problem is i need it in my hidden field.

Here is my loop in case you need to see it

  while ($stmt->fetch()){
      $id_static[] = $id_ai;      
      foreach($id_static as $k => $v){    
   } 

Below is my JQuery Ajax: Code

  <script>
    function update(gcc , g) {
      var idgap = $('#g').val();           
             jQuery.ajax({                   
                type: 'POST',
                 url: 'gap2.php',
                 dataType: 'json',
                 data:  {  gcc : gcc , idgap : idgap } ,                    
                 success: function(response){                
                 console.log(response);                     
                 jQuery('.response').html(response);                    
                }
           });     
        } 
     </script>

Ok what I've done in the Interim. Combined my values to 1 with a , separating <Select><option value='i3000,$id_static[$k]'>i3000</option> and split value in my PHP as below.

    $str = $_POST['gcc'];
    $split = explode("," , $str);

    $gcc =   $split[0];
    $g = $split[1];

Must be a better way to use AJAX for loops. My only goal is to make changes to PAGE without refreshing PAGE. Next issue is taking PHP result echo to ajax without changing <div> for each row.

You are forgetting to put PHP tags around the strings you want interpolated.

 <form>
       <div class='select-style'>
  <select  onChange='update(this.value)' id='gcc' name='gcc' class='form-control'>                        
        <option value='i2000'>i 2000</option>
        <option value='<?php $id_static[$k] ?>'>i 3000</option>
        <option value='i4000'>i 4000</option>
  </select>  
      <input  type='hidden'  id='g' name='g' value='<?php $id_static[$k] ?>'>
  </div>           
  </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM