亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將sql添加到javaScipt函數中?

如何將sql添加到javaScipt函數中?

繁星淼淼 2023-09-11 16:34:15
我希望用戶的輸入進入數據庫,但它不起作用。我有一個 sql 代碼工作塊,可以將數據插入數據庫,但運行時我不斷收到“未定義”錯誤。這是完整運行的sql代碼<?phprequire 'creationlink.php';$quizname = $_POST['name'];$quest = $_POST['question'];$ans1 = $_POST['answer1'];$ans2 = $_POST['answer2'];$ans3 = $_POST['answer3'];$ans4 = $_POST['answer4'];$correct = $_POST['A'];  //initiates connection with database with creation file  $sql = "INSERT INTO Quiz(question, answer1, answer2, answer3, answer4, correct) VALUES (?, ?, ?, ?, ?, ?)";  if($stmt = mysqli_prepare($conn, $sql)){      // Bind variables to the prepared statement as parameters      mysqli_stmt_bind_param($stmt, "ssssss", $quest, $ans1, $ans2, $ans3, $ans4, $correct);      // Attempt to execute the prepared statement      if(mysqli_stmt_execute($stmt)){          echo "Records inserted successfully.";      } else{          echo "ERROR: Could not execute query: $sql. " . mysqli_error($conn);      }  }  else{      echo "ERROR: Could not prepare query: $sql. " . mysqli_error($conn);  }  ?>這是我希望應用 sql 的代碼,它只是一個簡單的測驗模板,根據用戶輸入進行迭代。
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

在您的 HTML 中,所有<input>元素都在 之外<form>,因此您會收到錯誤undefined。要解決該錯誤,請將您的<input>元素移動到<form>標簽中。嘗試如下替換您的 html 代碼。


<html>

  <body>

    <form id='namennumber' action="includes/dbquestions.php" method="post">

      <label for="quizname"><b>Quiz name</b></label>

      <input name="quizname" type="text" placeholder="Enter quizname" required>

      <div id="myHTMLWrapper">

      </div>

    </form>

    <div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">

      <div id="questionform" class="modal">

        <form action="includes/dbquestions.php"> //moved all the input elements

          <label for="question"><b>Enter a question</b></label>

          <input name="question" type="text" placeholder="Enter a question" value="question" required>

          <br>

          <label for="answer1">Possible answer A</label>

          <input type="text" name="answer1" placeholder="Enter a possible answer" value="answer1" required>

          <br>

          <label for="answer2">Possible answer B</label>

          <input type="text" name="answer2" placeholder="Enter a possible answer" value="answer2" required>

          <br>

          <label for="answer3">Possible answer C</label>

          <input type="text" name="answer3" placeholder="Enter a possible answer" value="answer3" required>

          <br>

          <label for="answer4">Possible answer D</label>

          <input type="text" name="answer4" placeholder="Enter a possible answer" value="answer4" required>

          <br>


          <h3>Correct answer</h3>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />

          <label for="A">A</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />

          <label for="B">B</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />

          <label for="C">C</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D" />

          <label for="D">D</label>

          <br>

          <button style="width:auto;" id="enter" class="btn btn-primary big-btn">Save</button>

        </form>

      </div>

    </div>

    <script>

      var wrapper = document.getElementById("myHTMLWrapper");


      var number = prompt("Enter the number of questions");

      var myHTML = '';

      for (var i = 0; i < number; i++) {

        myHTML += '<span class="test">Question ' + (i + 1)

        myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +

          '</span><br/><br/>';

      }


      wrapper.innerHTML = myHTML


      function toggle(cont) {

        var cont = document.getElementById('cont');

        if (cont.style.display == 'block') {

          cont.style.display = 'none';


          document.getElementById(ele.id).value = 'Show DIV';

        } else {

          cont.style.display = 'block';

          document.getElementById(ele.id).value = 'Hide DIV';

        }

      };

    </script>

  </body>

</html>

請注意,您對HTML 中的action兩個s使用相同的參數, 。<form>見下文。


action="includes/dbquestions.php"

由于您在服務器端代碼中使用,因此$_POST您必須指定methodas ,因為 a 的默認方法是 is和 not 。post<form><form>getpost


更新 根據您在評論中發布的錯誤,我合并了您使用的兩種表格。


<html>

  <body>

    <form id='namennumber' action="includes/dbquestions.php" method="post">

      <label for="quizname"><b>Quiz name</b></label>

      <input name="quizname" type="text" placeholder="Enter quizname" required>

      <div id="myHTMLWrapper">

      </div>

    <div style="border:solid 1px #ddd; padding:10px; display:none;" id="cont">

      <div id="questionform" class="modal">

          <label for="question"><b>Enter a question</b></label>

          <input name="question" type="text" placeholder="Enter a question" value="question" required>

          <br>

          <label for="answer1">Possible answer A</label>

          <input type="text" name="answer1" placeholder="Enter a possible answer" value="answer1" required>

          <br>

          <label for="answer2">Possible answer B</label>

          <input type="text" name="answer2" placeholder="Enter a possible answer" value="answer2" required>

          <br>

          <label for="answer3">Possible answer C</label>

          <input type="text" name="answer3" placeholder="Enter a possible answer" value="answer3" required>

          <br>

          <label for="answer4">Possible answer D</label>

          <input type="text" name="answer4" placeholder="Enter a possible answer" value="answer4" required>

          <br>


          <h3>Correct answer</h3>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="A") echo "checked"; ?> value="A" />

          <label for="A">A</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="B") echo "checked"; ?> value="B" />

          <label for="B">B</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="C" )echo "checked"; ?> value="C" />

          <label for="C">C</label>

          <br>

          <input type="radio" name="A" <?php if (isset($answer) && $answer=="D" )echo "checked"; ?> value="D" />

          <label for="D">D</label>

          <br>

          <button style="width:auto;" id="enter" class="btn btn-primary big-btn">Save</button>

        </form>

      </div>

    </div>

    <script>

      var wrapper = document.getElementById("myHTMLWrapper");


      var number = prompt("Enter the number of questions");

      var myHTML = '';

      for (var i = 0; i < number; i++) {

        myHTML += '<span class="test">Question ' + (i + 1)

        myHTML += '<input type="button" value="Content" id="bt" onclick="toggle(\'cont\')">' +

          '</span><br/><br/>';

      }


      wrapper.innerHTML = myHTML


      function toggle(cont) {

        var cont = document.getElementById('cont');

        if (cont.style.display == 'block') {

          cont.style.display = 'none';


          document.getElementById(ele.id).value = 'Show DIV';

        } else {

          cont.style.display = 'block';

          document.getElementById(ele.id).value = 'Hide DIV';

        }

      };

    </script>

  </body>

</html>



查看完整回答
反對 回復 2023-09-11
  • 1 回答
  • 0 關注
  • 87 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號