为什么我的按钮没有将数据发布到我的mysql数据库?

时间:2014-12-06 19:26:30

标签: javascript php html

我这里有一个网页表单,通过javascript和php将数据提交到变量中,并将其传递给我的mysql数据库。然而,它没有这样做,按钮正在抛出错误。

HTML

        

   <link rel='stylesheet' type='text/css' href='test.css'/>
   <script type='text/javascript' src='jquery.js'></script>
    <script type='text/javascript' src='script.js'></script>



</head>

        <body><form>
    Strain name:<input type="text" id="strainName" />
    <br />
    Plant Generation:<input type="text" id="generation" />
    <br />
    Vegetation Date:<input type="date" id="vegDate" />
    <br />
    Flower Date:<input type="date" id="flowerDate" />
    <br />

    <input type="button" id="submit" value="Submit" onclick='submit()' />
     </form>
     <div id="sampleOutput"></div>


        <table border="1"  style="background-color:#66CC33;border-collapse:collapse;border:1px solid #000000;color:#000000;width:25%" cellpadding="3" cellspacing="3">
<tr>
    <td>Plant A1</td>
    <td>Plant A2</td>
</tr>
<tr>
    <td>Plant B1</td>
    <td>Plant B2</td>
</tr>
</table>
</body>
</html>

JS

function submit() {

var strainName = $('#strainName').val();
var generation = $('#generation').val();
var vegDate = $('#vegDate').val();
var flowerDate = $("#flowerDate").val();

$.post('postdata.php',{SN:strainName, GEN:generation, VD:vegDate, FD:flowerDate}, function(data)  {



});

PHP

<?php 

$STRAINNAME = $_POST['SN'];
$GENERATION = $_POST['GEN'];
$VEGDATE = $_POST['VG'];
$FLOWERDATE = $_POST['FD'];
$GENERATION= (int)$GENERATION;

$databaseConnVar = mysqli_connect('localhost',
                                'jackigsd_jack',
                                'Asdfgh13',
                                'jackigsd_flowerRoom')
                                or die('Error Connecting to Mysql Database server');

$query = "INSERT INTO flowerRoom (Strain Name, Generation, VegDate, FlowerDate)"."VALUES ('$STRAINNAME', '$GENERATION', '$VEGDATE', '$FLOWERDATE')";                                

$result = mysqli_query($databaseConnVar, $query)
or die('Error Connecting to Mysql Database server');                

    mysqli_close($databaseConnVar);

            print_r("Is this thing on?");                   

// if ($GENERATION < 16 ){echo $STRAINNAME . " is not old enough to drive";
// }else    {echo $STRAINNAME . " is old enough to Rock";}

?>

1 个答案:

答案 0 :(得分:0)

你可以使用ajax和序列化,并打印错误。

$.ajax({
  type: "POST",
  url: "postdata.php",
  data: $("form").serialize(),
  success: function(msg){
        console.log( "Data Saved: " + msg );
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
     console.log(textStatus);
  }
});

修复从PHP访问数据$ _POST的所有密钥(示例):

$STRAINNAME = $_POST['strainName'];