简体   繁体   中英

Value is not entered in mysql database through Javascript function

In the code given below, javascript function not working, in phpmyadmin - only package is entered but $clothes and $totamt is not entered in mysql database. Why it is so?

<?php   
mysql_connect("localhost","root","");
mysql_select_db('test');
if(isset($_REQUEST['submit']))
{
$name=$_REQUEST['name'];
@$packages=$_REQUEST['packages'];

$abc=mysql_query("insert into cusrec(name,packages,totamt,clthpackage)values('$name','$packages','$totamt','$clothes')");
}
?>
<html>
<head>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('input[name=packages]').function(slab(abc){
    var slabOption=slab.options[slab.selectedIndex];
   if(slabOption == "aslab"){
       <?php $clothes = 15; ?>
       <?php $totamt = 7500; ?>
   }
   else if(slabOption == "bslab"){
       <?php $clothes = 10; ?>
       <?php $totamt = 6000; ?>
   }
   else if(slabOption == "cslab"){
       <?php $clothes = 5; ?>
       <?php $totamt = 4000; ?>
   }
   else if(slabOption == "dslab"){
       <?php $clothes = 2; ?>
       <?php $totamt = 2000; ?>
   }
});
});
</script>
</head>
<body>
<form>
<table><tr>
    <td>Name</td>
    <td><input type="text" name="name" required="required"></td>
    </tr>
    <tr>
    <td>Packagaes</td>
      <td>
      <select name="packages" onChange="slab(this)">
      <option value="aslab" />Slab A 15 Clothes for 7500/-
      <option value="bslab" />Slab B 10 Clothes for 6000/-
      <option value="cslab" />Slab C 5 Clothes for 4000/-
      <option value="dslab" />Slab D 2 Clothes for 2000/-
      </select>
      </td>      
      </tr>
      <tr>
        <td colspan="2"><center><input type="submit" name="submit"value="Submit"></center></td>
        </tr>
    </table>
</form>
</body>
</html>

when I got to view my database values, then filed of package is correctly entered according to salb but $totamt and $clothes remain blank. Not able to understand why it is so?

The conditions you have written in javascript code is of no use. They make no sense. Take them inside the post handler code in php and set the variables there.

like

if(isset($_REQUEST['submit']))
{
    $name=$_REQUEST['name'];
    $packages=$_REQUEST['packages'];

    if($packages == "aslab"){
        $clothes = 15; 
       $totamt = 7500; 
    }
    else if($packages == "bslab"){
       $clothes = 10; 
       $totamt = 6000;
    }
    else if($packages == "cslab"){
       $clothes = 5; 
       $totamt = 4000;
    }
    else if($packages == "dslab"){
       $clothes = 2; 
       $totamt = 2000; 
    }

    $abc=mysql_query("insert into cusrec(name,packages,totamt,clthpackage)values('$name','$packages','$totamt','$clothes')");
}

and you can remove the javascript code completely as it makes no sense to set PHP variables inside JS by reading the selected option.

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