简体   繁体   English

textarea文本未插入php数据库mysql

[英]textarea text not inserting to php database mysql

hi guys pls help me whit this option(textarea) not submiting to database php mysql here are my codes tnx. 嗨,大家好,请帮助我,这个选项(textarea)不能提交给数据库php mysql,这是我的代码tnx。 it has a Javascript function. 它具有Javascript函数。 i need to send the option(textarea) to the database php/mysql. 我需要将option(textarea)发送到数据库php / mysql。 text not inserting. 文字未插入。 or it can be numbers of how many food they selected. 或者可以是他们选择了多少食物的数量。

select.html 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 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 connection.php

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

js.1.js (JavaScript) 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(); 
   }); 
 }); 

}); });

Start by cleaning up your code and making it well formed, ie nest it accurately, and close opening tags appropriately), like this. 首先,清理代码并使其结构正确(即准确地将其嵌套,并适当地关闭打开的标签),就像这样。 For example, you missed putting in the <head> tag, and the </select> was below the </form> : 例如,您错过了放入<head>标记,而</select></form>下方的情况:

select.html 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>

Later when you get good you can get sloppy about this if you want, but while you are starting out make it very clean. 以后,当您恢复健康时,可以根据需要对此放任自流,但是在开始时,请使其变得非常干净。 Notice how I lined up onclick to make things more readable. 请注意,我如何排列onclick使其更具可读性。

Now once you have it clean, look at it line by line, word by word. 现在,一旦清理干净,就逐行逐字查看。 Do you understand every single thing? 你了解每一件事吗? If not google it until you do. 如果不是谷歌,直到你做。 There are good references to all of this stuff on the web, far better than I or anyone else can provide here. 网络上所有这些东西都有很好的参考,远胜于我或任何人在这里提供的东西。 If we solve your immediate problem we rob you of learning how to do this stuff yourself. 如果我们解决了您的迫在眉睫的问题,我们就会劫持您自己学习如何做这些事情。 But glad you ask, and glad to give you some pointers. 但是很高兴您提出问题,也很高兴为您提供一些建议。

Edit: One of the html traps and why many professionals mostly use Xhtml, and fall back to html5 only if needed for html5 only features, is that while a particular browser might work with a particular malformed set of tags, (like your <select> or <head> issues above) another browser might not do so well and gag on them. 编辑:html陷阱之一,以及为什么许多专业人士大多使用Xhtml并仅在仅html5功能需要时才使用html5的原因是,虽然特定的浏览器可能会使用特定格式错误的标记集,(例如您的<select>或上面的<head>问题),其他浏览器可能做得不好,因此无法正常使用。 By forcing tags to always be correctly nested as xhtml does, and which can be easily discovered with a xhtml validator , it removes any chance that a browser will or will not correctly interpret the html. 通过强制标签始终像xhtml一样正确地嵌套,并且可以使用xhtml 验证程序轻松地发现标签,它消除了浏览器将正确解释HTML的任何可能性。 Another thing that's missing from your html is the first line, but I'm guessing you just left that out above for brevity. 您的html缺少的另一件事是第一行,但我想为了简洁起见,您只是将其省略了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM