如何使用<select>标签中的值更新数据库中的值 - (非$ _POST)</select>

时间:2013-01-16 22:26:17

标签: php database joomla joomla1.5

我想从<select><input> 标记中获取值,但使用函数 onclick按钮,而不使用{{1 }}。我做了一个尝试,但我有语法堆栈。如果$_POST是这样的话:

表单中的按钮:

$_POST

我的更新查询:

<input class="" name="submit" type="submit" value="UPDATE" />

现在我正在尝试这样做:

表单中的按钮:

if (isset($_POST['submit']) or isset($_GET['submit'])){
$db =& JFactory::getDBO();
$query = "UPDATE table
             SET name = '".$_POST["name"]."',
                 lastname = '".$_POST["lastname"]."',
                 rank = '".$_POST["rank"]."'
           WHERE id=1";
$db->setQuery($query);
$db->query();}

我的更新查询:

<input class="" name="submit" type="submit" value="UPDATE" onclick="update()" />

但是调用function update(){ $db =& JFactory::getDBO(); $query = "UPDATE table SET name = ???, lastname = ???, rank = ??? WHERE id=1"; $db->setQuery($query); $db->query();} <select>标记名称的语法是什么?或者换句话说他们的价值观。

3 个答案:

答案 0 :(得分:4)

您将客户端(JS)上运行的代码与服务器端(PHP)上运行的代码混淆。 PHP在PHP完成后运行 - 因此除非您提交表单(POST / GET)或使用AJAX,否则不能从JS“调用”PHP中的函数。

答案 1 :(得分:3)

如果您只想点击按钮而不刷新或重定向浏览器,TYOU必须使用$ .ajax

1)阅读http://api.jquery.com/jQuery.ajax/

2)在你的按钮上添加一个eventhandler onclick =“update();”

3)创建一个类似于ajax的东西:

function request(variable1,variable2,variable3){

        var request = $.ajax({
          url: "/server.php", // The address of you php script that will handle the request 
          type: "POST", // Method type GET / POST in this case POST (Similar to form submission methods....)
          data: { // What you send to the server 'VariableName' : VariableValue, in this case you assign the varaiables that were passed to the request function.
                'var1': variable1,
                'var2' : variable2,
                'var3': variable3
                },
          dataType: "json" // The response type you expect from the server
        })

        request.done(function(msg) // Function being called when everything is ok and server sends back data
        {
             console.log(msg) // handle the reply / data
        })

        request.fail(function(jqXHR, textStatus) // Something went wrong...
        {
            console.log(textStatus); // See the error report
            console.log(jqXHR);
        })

        request.complete(function(){
            console.log("Ajax request complete!"); // This will always be executed when a request has completed even if it failed. (Executes after .done and .fail)
        })


}

所以你可以在你点击按钮时调用的更新函数中执行此操作:

function update()
{
 var val1 = $.('#selectbox').val();
 var val2 = $.('#inputbox').val();
 var val3 = $.('#textarea').val();

 new request(val1,val2,val3);

}

请求/变量将使用POST方法发送,因此在您的PHP脚本中 您可以像处理表单一样处理它们

if(isset($_POST['var1']) && isset($_POST['var2']) && isset($_POST['var3']))
{
   $serverReply = doThings($_POST['var1'],$_POST['var2'],$_POST['var3']);

   //and this is how you reply to your client/browser in json format

   echo json_encode($serverReply);
}

确保在有关ajax通信的深度教程中查看更多内容。 网上有很多。

答案 2 :(得分:1)

onclick调用函数javascript

函数javascript用ajax实现

示例:

$("#submitButtonId").click(function() {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#idForm").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    return false; // avoid to execute the actual submit of the form.
});