textarea文本没有插入php数据库mysql

时间:2014-01-25 01:38:03

标签: javascript php jquery html mysql

嗨伙计请帮助我这个选项(textarea)没有提交到数据库php mysql这里是我的代码tnx。它有一个Javascript函数。我需要将选项(textarea)发送到数据库php / mysql。文字没有插入。或者它可以是他们选择了多少食物的数量。

select.html

<html lang="en">
<head>
<title>Catering Service</title>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="js/js.1.js" type="text/javascript"></script>
</head>
<body>
     <form action="submit.php" method="post">
         <select multiple="multiple" class="options" id="textarea">
         <option value="foodA">foodA</option>
         <option value="foodB">foodB</option>
         <option value="foodC">foodC</option>
         <option value="foodD">foodD</option>
      <option value="foodE">foodE</option>
     </select>


         <button type="button" id="copy">Copy</button>
         <button type="button" id="remove">Remove</button>

          <!-- note how multiple select name must be set -->
          <select id="textarea2" multiple class="remove" name="food[]"> 
          </select>

          <input type="submit" name="submit" />
          </form>
          </body>
           </html>

submit.php

<?php
  include 'connection.php';

foreach ($_POST['food'] as $food){
     $food == "foodA" ? $foodA = $food : $foodA = '';
     $food == "foodB" ? $foodB = $food : $foodB = '';
     $food == "foodC" ? $foodC = $food : $foodC = '';
     $food == "foodD" ? $foodD = $food : $foodD = '';
     $food == "foodE" ? $foodE = $food : $foodE = '';


if(!$_POST['submit']) {
    echo "please fill out the form";
    header('Location: select.html');
}
else {
    $sql = "INSERT INTO remove(foodA, foodB, foodC, foodD, foodE) VALUES (?,?,?,?,?);";
    $stmt = mysqli_prepare($conn, $sql);
    mysqli_stmt_bind_param($stmt,"sssss",$foodA,$foodB,$foodC,$foodD,$foodE);
    mysqli_stmt_execute($stmt);
    echo "User has been added!";
    header('Location: select.html');
}

connection.php

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "copy";
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
 ?>

js.1.js(JavaScript)

   $(function(){ 
   $("#copy").on("click", function(){ 
   $(".options option:selected").each(function({
   $("#textarea2").append('<option selected>'+$(this).text()+'</option>');
   $('option:selected', "#textarea").remove(); 
  });
}); 
    $("#remove").on("click", function(){ 
    $(".remove option:selected").each(function(){
    $("#textarea").append('<option>'+$(this).text()+'</option>');
    $('option:selected', "#textarea2").remove(); 
   }); 
 }); 

});

1 个答案:

答案 0 :(得分:0)

首先清理你的代码并使其形成良好,即准确嵌套,然后适当地关闭开放标签),就像这样。例如,您错过了<head>标记,而</select>低于</form>

select.html

<html lang="en">
  <head>
    <title>Catering Service</title>
    <meta charset="utf-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script src="js/js.1.js" type="text/javascript"></script>
  </head>
  <body>
    <form action="submit.php" method="post">

      <select multiple="multiple" name="food" class="options" id="textarea">
        <option name="foodA" value="foodA">foodA</option>
        <option name="foodB" value="foodB">foodB</option>
        <option name="foodC" value="foodC">foodC</option>
        <option name="foodD" value="foodD">foodD</option>
        <option name="foodE" value="foodE">foodE</option>
      </select>

      <button type="button" id="copy"   onclick="yourFunction()">Copy</button>
      <button type="button" id="remove" onclick="yourFunction()">Remove</button>

      <select id="textarea2" multiple class="remove" name="food">
        <input type="submit" name="submit" />
      </select>
    </form>
  </html>

稍后当你变得好的时候,如果你愿意的话,你可以对此有所了解,但是当你开始时,要让它变得非常干净。注意我是如何排队的,以使事情更具可读性。

现在一旦你把它弄干净,一行一字地逐行查看。你了解每一件事吗?如果不是谷歌它,直到你这样做。在网上有很好的参考资料,远比我或其他人在这里提供的要好得多。如果我们解决您的直接问题,我们会剥夺您自己学习如何做这些事情的麻烦。但很高兴你问,并很乐意给你一些指示。

编辑:其中一个html陷阱以及为什么许多专业人士大多使用Xhtml,并且仅在html5功能需要时才回退到html5,这是因为特定浏览器可能会使用特定格式错误的标签集(例如您的上面的<select><head>问题)另一个浏览器可能效果不佳并且对它们不屑一顾。通过强制标记始终被正确嵌套为xhtml,并且可以使用xhtml validator轻松发现,它消除了浏览器将或不会正确解释html的任何机会。你的html中缺少的另一件事是第一行,但我猜你只是为了简洁而把它留在了上面。