从按钮按下事件运行SQL查询

时间:2022-11-19 22:06:40

I am using PHP and am wanting to run a sql query on a button press event. I have this synatx, but the problem I have is nothing happens when the button is pressed! I opened the developer console and I see no action from where the button is pressed.

我正在使用PHP,并希望在按钮按下事件上运行sql查询。我有这个系统,但是我的问题是当按下按钮时什么都不会发生!我打开了开发人员控制台,在按下按钮的地方看不到任何动作。

What did I improperly code here and how shoudl I change it so that it executes as I expect?

我在这里做了什么不正确的编码?我应该如何修改它,使它像我期望的那样执行?

    <html>
<tbody> 
Start Date:<input type="date" name="startdate">
End Date:<input type="date" name="enddate">
<input type="submit" name="submit" value="Query DB">
</tbody>
</html>
<?php 

if(isset($_POST['submit'])){
    $startdate = $_GET['startdate'];
    $enddate = $_GET['enddate'];
    $option = array();
    $option['driver'] = 'mssql'; 
    $option['host'] = 'IP Address'; 
    $option['user'] = 'user'; 
    $option['password'] = 'pwd'; 
    $option['database'] = 'DB'; 
    $option['prefix'] = '';
    $db = JDatabase::getInstance( $option );
    $query = $db->getQuery(true);
    $query = "Select Top 1 firstname from userinformation where hiredate >= '$startdate' and termdate < '$enddate'"; 
    $db->setQuery($query); 
    $query = $db->loadObjectList();
}
?>
</html>

2 个解决方案

#1


2  

You need a jQuery AJAX post method like this on the client-side:

您需要在客户端上使用jQuery AJAX post方法:

$("#form_id").submit(function( event ) {
  $.post( "", {startdate: $("#form_id :input[name='startdate']"), enddate: $("#form_id :input[name='enddate']");} , function( data ) {
    // handle the returning data if you need
  });
});

$("#form_id").submit(function( event ) listens for the form submission event of the from with id="from_id".

$(" # form_id”)。submit(函数(event)侦听来自id="from_id"的表单提交事件。

$("#form_id :input[name='startdate']") gets the input value.

$(#form_id:input[name='startdate'])获取输入值。

Then lastly jQuery.post() method sends a JSON object like {startdate: "01.01.2016", enddate: "12.31.2016"} to the server.

然后,最后jQuery.post()方法发送一个JSON对象,比如{startdate:“01.01.2016”,enddate:“12.31.2016”}到服务器。

I wrote "" as the target of POST method, it could be like "/myScript.php" in your case.

我把“”作为POST方法的目标,可以是“/myScript”。php”在你的情况中。

On server-side $_POST variable of PHP parses the request automatically.

在PHP的服务器端$_POST变量上,自动解析请求。

Also you need to change these two lines on the server-side:

还需要在服务器端更改这两行:

$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];

because you are using POST method to send your data and GET method.

因为你使用POST方法来发送数据和获取方法。

Note: Question tagged with jquery so I assumed that request should be handled by jQuery.

注意:问题标记为jquery,因此我假设请求应该由jquery处理。

#2


1  

A submit button needs to be inside tags. Specifically, you will need to have <form method="POST"> at a minimum as the default is GET which would put the data in $_GET instead of $_POST. Actually, I see you have an inconsistency here - some $_GET and some $_POST - that definitely won't work right - needs to be all one or the other.

提交按钮需要位于标签内部。具体地说,您需要将

至少设置为默认的GET,将数据放在$_GET而不是$_POST中。实际上,我看到你有一个不一致的地方-一些$_GET和$_POST - - -这肯定是行不通的- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

To "keep" the values on reload of the form, change the input lines to:

若要“保留”窗体重载上的值,请将输入行更改为:

Start Date:<input type="date" name="startdate" value=<?=$_POST['startdate']?>">
End Date:<input type="date" name="enddate" value="<?=$_POST['enddate']?>">

#1


2  

You need a jQuery AJAX post method like this on the client-side:

您需要在客户端上使用jQuery AJAX post方法:

$("#form_id").submit(function( event ) {
  $.post( "", {startdate: $("#form_id :input[name='startdate']"), enddate: $("#form_id :input[name='enddate']");} , function( data ) {
    // handle the returning data if you need
  });
});

$("#form_id").submit(function( event ) listens for the form submission event of the from with id="from_id".

$(" # form_id”)。submit(函数(event)侦听来自id="from_id"的表单提交事件。

$("#form_id :input[name='startdate']") gets the input value.

$(#form_id:input[name='startdate'])获取输入值。

Then lastly jQuery.post() method sends a JSON object like {startdate: "01.01.2016", enddate: "12.31.2016"} to the server.

然后,最后jQuery.post()方法发送一个JSON对象,比如{startdate:“01.01.2016”,enddate:“12.31.2016”}到服务器。

I wrote "" as the target of POST method, it could be like "/myScript.php" in your case.

我把“”作为POST方法的目标,可以是“/myScript”。php”在你的情况中。

On server-side $_POST variable of PHP parses the request automatically.

在PHP的服务器端$_POST变量上,自动解析请求。

Also you need to change these two lines on the server-side:

还需要在服务器端更改这两行:

$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];

because you are using POST method to send your data and GET method.

因为你使用POST方法来发送数据和获取方法。

Note: Question tagged with jquery so I assumed that request should be handled by jQuery.

注意:问题标记为jquery,因此我假设请求应该由jquery处理。

#2


1  

A submit button needs to be inside tags. Specifically, you will need to have <form method="POST"> at a minimum as the default is GET which would put the data in $_GET instead of $_POST. Actually, I see you have an inconsistency here - some $_GET and some $_POST - that definitely won't work right - needs to be all one or the other.

提交按钮需要位于标签内部。具体地说,您需要将

至少设置为默认的GET,将数据放在$_GET而不是$_POST中。实际上,我看到你有一个不一致的地方-一些$_GET和$_POST - - -这肯定是行不通的- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

To "keep" the values on reload of the form, change the input lines to:

若要“保留”窗体重载上的值,请将输入行更改为:

Start Date:<input type="date" name="startdate" value=<?=$_POST['startdate']?>">
End Date:<input type="date" name="enddate" value="<?=$_POST['enddate']?>">