Question

i have this code. And I need add one more sub_season var. How add more var? I am tray looking in google but no results. Thanks you if you help me!!!

<?php
echo"<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
<script type='text/javascript'><!--
$(document).ready(function() {
  // when the user submit a form
  $('form').submit(function() {
    var url = $(this).attr('action');       // gets the url from 'action' attribute of the current form

    // define data to be send to PHP
    // with the key 'pag' and the value selected in the select list (with id='pag')
    var data = 'season='+ $('#season').val();

    // adds a 'loading...' notification, load the content from url (passing the data)
    // and place it into the tag with id='content'
    $('#content').html('<h4>Loading...</h4>').load(url, data);
    return false;
  });
});
--></script>";
echo"<div id='content'>Default</div>
<form action='script.php' method='get'>
 Select course:
 <select name='season' id='season'>
  <option value='season'>season</option>
  <option value='php'>PHP</option>
  <option value='ajax'>Ajax</option>
 </select> 

<select name='sub_season' id='sub_season'>
  <option value='sub_season'>sub_season</option>
  <option value='php'>PHP</option>
  <option value='ajax'>Ajax</option>
 </select>
 <input type='submit' id='submit' value='Submit' />
</form>";
?>
Was it helpful?

Solution

You can choose to send the data via GET or POST

// send multiple vars via post
var data_post = {};
data_post.season = $('#season').val();
data_post.sub_season = $('#sub_season').val();

// send multiple vars via get
var data_get = 'season='+$('#season').val()+'&sub_season='+$('#sub_season').val();

and catch the vars in script.php like this:

<?PHP
    if(isset($_POST['season']))
        echo '<h1>'.$_POST['season'].' [post]</h1>';
    if(isset($_POST['sub_season']))
        echo '<h1>'.$_POST['sub_season'].' [post]</h1>';

    if(isset($_GET['season']))
        echo '<h1>'.$_GET['season'].' [get]</h1>';
    if(isset($_GET['sub_season']))
        echo '<h1>'.$_GET['sub_season'].' [get]</h1>';
?>

In case I missunderstood you... and you want to send multiple selected values of the sub_season field, you should iterate over the selected values

// via GET
var data_get = 'season='+$('#season').val(); 
$('#sub_season :selected').each(function(){ 
  data_get += '&sub_season[]='+ $(this).val(); 
});

// via POST
var data_post = {};
data_post.season = $('#season').val(); 
data_post.sub_season = [];
$('#sub_season :selected').each(function(){ 
  data_post.sub_season.push($(this).val()); 
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top