使用onclick函数插入数据库 [英] Insert into database using onclick function

查看:86
本文介绍了使用onclick函数插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一种基于课程明智的问题搜索引擎,主题明智的是输入关键字或问题。
这里我根据3个表的搜索项查询数据库, table_one table_two ,以及 Table_Three中。代码如下

I'm developing a kind of question search engine based on Course wise, Subject wise by entering the keyword or question. Here I am querying the database based on search term against 3 tables namely table_one, table_two, and table_three. Code as follows

<?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> ";
}
}
}
?>

当我点击Add to Question Bank链接时,我正在调用以下addQuestion()函数。

I'm Calling the following addQuestion() function when i click the Add to Question Bank link.

    <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任何帮助都可能会有所帮助。

The script above displaying confirmation box when i click the button, My Question is, After confirmation I want to insert my question into the new table in the database and display message like "Question added" in front of the question permanently as i know i can't write PHP inside Jquery function Any help may appreciated.

推荐答案

Onclick - 你需要用ajax做到这一点。基本上,你需要PHP和javascript参与。您可以使用类似JS库的Jquery来轻松支持ajax。

Onclick - You need to do that with ajax. So basically, you need PHP plus javascript involved. You can use Jquery of similar JS library for easy ajax support.

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

Just and example with jquery library version 1.11.2 how to include:

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

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

For example, if this is your input field you want to save and button for submitting:

<input id="title" name="title" />

<input type="submit" value="Save">

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

Change it to button and give it javascript save() function (can be any name you give).

< input type =buttononclick =save($('#title')。val()); value =Save>

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

In this example, I added 1 param to that save function, which is supposed to grab a value from html input filed with id "title". On this page, there that html is, you need to include mentioned jquery(or similar) library and also include piece of javascript function for generating ajax request, which is named "save" here.

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

If you included jquery library, you must call javascript function for saving your data before your tag:

<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函数执行时,它会向你的path / yourfile发送POST请求。 php

When javascript function you named save() will execute, it will send POST request to yourpath/yourfile.php

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

There, you can easily get your POST data by in yourpath/yourfile.php:

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

如果你想用GET发送它,你可以轻松替换 POST GET

If you want to send it with GET, you easily replace POST with GET:

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

以及您编写的.php文件:

and also in .php file you write:

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

这篇关于使用onclick函数插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆