How to insert values of dynamic fields into database using PHP ,MySqL and JQuery

246 Views Asked by At

I am trying to make a country -state selector in Jquery which should submit values in db using PHP. Here is the script:

<script type="text/javascript">
var state_arr=new Array("Jammu and Kashmir","Delhi","Uttar Pradesh","Tamil nadu","Maharashtra","Karnataka","West Bengal","Punjab","Haryana");
var s_a = new Array();
s_a[0]="";
s_a[1]="Srinagar|Jammu";
s_a[2]="New Delhi";
s_a[3]="Lucknow|Kanpur|Ghaziabad|Noida|Varanasi";
s_a[4]="Chennai";
s_a[5]="Mumbai|Pune|Aurangabad|Thane";
s_a[6]="Bangalore|Mysore";
s_a[7]="Kolkata";
s_a[8]="Chandigarh|Mohali";
s_a[9]="Gurgaon|Chandigarh";
function print_state(state){
    //given the id of the <select> tag as function argument, it inserts <option> tags
    var option_str = document.getElementById(state);
    option_str.length=0;
    option_str.options[0] = new Option('Select State','');
    option_str.selectedIndex = 0;
    for (var i=0; i<state_arr.length; i++) {
    option_str.options[option_str.length] = new Option(state_arr[i],state_arr[i]);
    }
}
function print_city(city, selectedIndex){
    var option_str = document.getElementById(city);
    option_str.length=0;    
    option_str.options[0] = new Option('Select City','');
    option_str.selectedIndex = 0;
    var city_arr = s_a[selectedIndex].split("|");
    for (var i=0; i<city_arr.length; i++) {
    option_str.options[option_str.length] = new Option(city_arr[i],city_arr[i]);
    }
}

Here is the PHP code which displays the selector:

<?php if (!isset($_POST['submit_val3'])) { ?>
Select the State:
<select onchange="print_city('city',this.selectedIndex);" id="state" name ="state"></select>
<br />
City:
<select name ="city" id ="city"></select>
<script language="javascript">print_state("state");</script>
<input type="submit" name="submit_val3" value="Submit" />
<?php } ?>

Here is PHP code for submission:

if(isset($_POST['submit_val3']))
{
  $city=$_POST['city'];
  $state=$_POST['state'];
 $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
 mysql_select_db(DB_NAME);
  $query="UPDATE register_emp SET state='$state',city='$city' WHERE ID = '" . $_SESSION['ID'] . "'";
  mysql_query($query,$dbc) or die('query failed');
  mysql_close();
}

My problem is that on clicking on Submit button,no values are being submitted to database.Where am I wrong here ?

1

There are 1 best solutions below

0
On BEST ANSWER

Please add a form tag and action , thats why its not submitting to php page.


    <?php if (!isset($_POST['submit_val3'])) { ?>
    <form method="post" action="yourphppage.php">  <!-- form tag and action page -->
    Select the State:
    <select onchange="print_city('city',this.selectedIndex);" id="state" name ="state">
    
City: <select name ="city" id ="city"> <script language="javascript">print_state("state"); <input type="submit" name="submit_val3" value="Submit" /> </form> <?php } ?>