【问题标题】:Insert into database using onclick function使用 onclick 函数插入数据库
【发布时间】:2015-04-06 12:12:11
【问题描述】:

我正在开发一种基于 Course wise, Subject wise 通过输入关键字或问题的问题搜索引擎。 在这里,我根据搜索词查询数据库,针对 3 个表,即 table_onetable_twotable_three。代码如下

<?php
 if(isset($_GET['submit']))
 {
   $query = $_GET['query'];
   $query = htmlspecialchars($query);
   $query = mysqli_escape_string($link,$query);
   $searchTerms = explode(' ', $query);
   $searchTermBits = array();
   foreach ($searchTerms as $term) {
    $term = trim($term);
    if (!empty($term)) {
        $searchTermBits[] = "question LIKE '%$term%'";
    }
  }

 $subject_id = $_GET['subject'];
 $course_id = $_GET['course'];
 $min_length = 1;
 if(strlen($query) >= $min_length)
 {

  $res = "SELECT id,course_id,subject_id,question,option_a,option_b,option_c,option_d,option_e,correct_ans,fmge_year,contributor FROM table_one
WHERE (".implode(' OR ', $searchTermBits).") AND (`subject_id` LIKE '%".$subject_id."%') AND (`course_id` LIKE '%".$course_id."%')
UNION ALL
SELECT id,course_id,subject_id,question,option_a,option_b,option_c,option_d,option_e,correct_ans,fmge_year,contributor FROM table_two 
WHERE (".implode(' OR ', $searchTermBits).") AND (`subject_id` LIKE '%".$subject_id."%') AND (`course_id` LIKE '%".$course_id."%')
UNION ALL
SELECT id,course_id,subject_id,question,option_a,option_b,option_c,option_d,option_e,correct_ans,fmge_year,contributor FROM table_three 
WHERE (".implode(' OR ', $searchTermBits).") AND (`subject_id` LIKE '%".$subject_id."%') AND (`course_id` LIKE '%".$course_id."%')";

$raw_results = mysqli_query($link,$res) or die (mysqli_error());
if(mysqli_num_rows($raw_results) > 0)   
{
echo "<h3 style='text-align:center;color:#3366CC'><span style='color:#000000'>Search Results For : </span> $query </h3>";
while($results = mysqli_fetch_array($raw_results))
{
echo "<div class='content'>";
echo"<h4 id=".$results['id'].">" .preg_replace("/".preg_quote($query, "/")."/i", "<span class=\"highlight\">$query</span>", $results['question']) . "</h4>";
echo"<p id=".$results['id']."><span style='padding-left:20px'>option A : " .$results['option_a']."</span> <br><span style='padding-left:20px'> option B : ".$results['option_b']."</span><br/><span style='padding-left:20px'>option C : ".$results['option_c'].
"</span><br><span style='padding-left:20px'>option D : ".$results['option_d']."</span><br><span style='padding-left:20px'> option E : ".$results['option_e']."</span><br><span style='color:#253E66;font-weight:bold;padding-left:20px'>Correct Ans : ".$results['correct_ans']. 
"</span><br><span style='padding-left:20px'>Question Year : ".$results['question_year']."</span><br><span style='padding-left:20px'>Contributor : ".$results['contributor']."</span><br />
<a onclick=addQuestion('".$results['id']."') href='#'><span class='button'>Add to Question Bank</span></a></p>";
echo "</div>";
}
}
else{

echo "<span style='height:21px;syle=background-color: #F1F0FF;font-size:25px;color:#CC0000'>Your search - $query - did not match any queries.</span> ";
}
}
}
?>

当我单击“添加到问题库”链接时,我正在调用以下 addQuestion() 函数。

    <script>
    function addQuestion(val)
    {
        var conf=confirm("Are you sure you want to add this question to Question Bank")

        if(conf){
             //Here I Want some code to update my database. 
              }
    }
</script>

当我单击按钮时,上面的脚本显示确认框, 我的问题是, 确认后,我想将我的问题插入到数据库中的新表中,并在问题前面永久显示“已添加问题”之类的消息,因为我知道我无法在 Jquery 函数中编写 PHP 任何帮助可能会不胜感激。

【问题讨论】:

  • 在 if(conf){ } 块中,您可以使用 jquery ajax 并将数据发送到 serverfile 并且响应您可以轻松显示添加的消息问题。
  • 好的!所以...你试过什么?有任何错误,还是只是在乞求我们提供一些插入代码?
  • 我是新手,对ajax一无所知,任何代码都可能有帮助。

标签: php jquery


【解决方案1】:

您可以通过包含 ajax 来实现这一点。 把可能看起来像下面这样的ajax代码:

if(conf){
    $.ajax({
           type: "POST",
           url: "$$phpfilepath",
           data: {param:'$$value'},
           success: function(data) {
        // do the message display code
           }
       });
}

不要忘记在 html 页面的 head 标签中包含 jquery cdn 链接。

【讨论】:

    【解决方案2】:

    您需要发送一个 ajax 请求。 您需要通过 post 或 get 方法将其发送到将返回 json 的 php 脚本,以便您可以在页面上更新结果。 上面的答案有一个使用 post 方法发送的示例 ajax 脚本: 如果您通过表单或数组提交数据,则需要对数据进行 sielized。

    这应该可以帮助你 http://www.w3schools.com/php/php_ajax_database.asp https://api.jquery.com/serialize/

    【讨论】:

      【解决方案3】:

      Onclick - 您需要使用 ajax 来执行此操作。所以基本上,你需要 PHP 加上 javascript。您可以使用类似 JS 库的 Jquery 来轻松支持 ajax。

      jquery 库版本 1.11.2 的示例和示例如何包含:

      <head>
      <script src="jquery-1.11.2.min.js"></script>
      </head>
      

      例如,如果这是您要保存的输入字段和提交按钮:

      <input id="title" name="title" />
      
      <input type="submit" value="Save">
      

      将其更改为按钮并为其提供 javascript save() 函数(可以是您提供的任何名称)。

      &lt;input type="button" onclick="save($('#title').val());" value="Save"&gt;

      在这个例子中,我向该保存函数添加了 1 个参数,它应该从 id 为“title”的 html 输入字段中获取一个值。 在这个页面上,那个 html 是,你需要包含提到的 jquery(或类似的)库,还需要包含一段用于生成 ajax 请求的 javascript 函数,这里命名为“save”。

      如果你包含了 jquery 库,你必须在你的标签之前调用 javascript 函数来保存你的数据:

      <script type"text/javascript">
      function save(){
          $.ajax({
              type: "POST",
              url: "yourpath/yourfile.php",
              data: {title: title},
              success: function(data) {
                  alert("Ajax save executed!");
              }
          });
      }
      </script>
      

      当你命名为 save() 的 javascript 函数将执行时,它会发送 POST 请求到 yourpath/yourfile.php

      在那里,您可以通过 yourpath/yourfile.php 轻松获取 POST 数据:

      if(isset($_POST['title'])){ 
      // do something with POST data, save in db.. (make sure to include security when inserting to db) 
      }
      

      如果您想使用 GET 发送,您可以轻松地将 POST 替换为 GET

      function save(){
              $.ajax({
                  type: "GET",
      

      还有你写的 .php 文件:

      if(isset($_GET['title'])){ 
      // do something with POST data, save in db.. (make sure to include security when inserting to db) 
      }
      

      【讨论】:

      • 我尝试如下,但 Ajax 代码不起作用&lt;script type="text/javascript"&gt; function addQuestion(val) { var conf=confirm("Are you sure you want to add this question to Question Bank"); if(conf){ $.ajax({ type: "POST", url: "localhost/my_folder/my_filename.php", data: {title: title}, success: function(data) { alert("Ajax save executed!"); } }); } } &lt;/script&gt;
      • 数据:{title:title} 错误。你传递了名为 val 的参数。它应该是这样的: data: {title: val}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      • 2023-01-12
      相关资源
      最近更新 更多