简体   繁体   English

PHP和JQuery Ajax网址

[英]PHP and JQuery ajax url

I am using a ready booking form source code and I would like to make some changes according to our needs. 我使用的是现成的预订表格源代码,我想根据我们的需要进行一些更改。

I quote below the source code that is important to be seen and not the whole source code of the file. 我在下面引用了很重要的源代码,而不是文件的整个源代码。 I would like to execute, as soon as a button is clicked, a mysqli_query to update variables on the database. 我想在单击按钮后立即执行mysqli_query来更新数据库上的变量。 So, I am trying to use Jquery and AJAX to make this happen. 因此,我正在尝试使用Jquery和AJAX来实现这一目标。

The code below shows a button Check Availability already defined which executes Javascript code and I added also my button "Book Now" and I would like to run also my code. 下面的代码显示了已经定义的“ Check Availability按钮,该按钮执行Javascript代码,并且我还添加了按钮“立即预订”,我也想运行我的代码。 See the code below: 请参见下面的代码:

<form id="bsSearchForm_<?php echo $_GET['index'];?>" action="<?php echo PJ_INSTALL_URL; ?>index.php?controller=pjFront&amp;action=pjActionCheck" method="post" >
<div class="row ">
      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 col-xss-12">
           <div class="form-group">
                <label for="address">Pickup Address</label>
                <input type="text" class="form-control" name="pickup" placeholder="Enter a location">
           </div>
      </div>

      <div class="form-group pjBsFormActions">
           <button type="submit" class="btn btn-primary"><?php echo("Check Availability"); ?></button>
           <button type="submit" class="btn btn-primary" onclick="checkClicked()"><?php echo("Book Now"); ?></button>
      </div><!-- /.form-group pjBsFormActions -->
</div>
</form>

Now at the same php file at the beginning I have defined this source code: 现在,在开头的同一个php文件中,我已经定义了以下源代码:

<script>
function checkClicked() {
    $.ajax({
        url: "test.php",
        type: 'POST',
        dataType: 'html'
    });
}
</script>

So, I would like to run my own PHP source code at an external php file like test.php and get the input field data from "pickup" and perform a mysqli_query on database. 因此,我想在外部PHP文件(如test.php上运行我自己的PHP源代码,并从“ pickup”获取输入字段数据,并在数据库上执行mysqli_query

However the code at test.php file is not executed at all. 但是,根本不会执行test.php文件中的代码。 I think that the problem is the form action parameter <?php echo PJ_INSTALL_URL; ?>index.php?controller=pjFront&amp;action=pjActionCheck 我认为问题在于表单操作参数<?php echo PJ_INSTALL_URL; ?>index.php?controller=pjFront&amp;action=pjActionCheck <?php echo PJ_INSTALL_URL; ?>index.php?controller=pjFront&amp;action=pjActionCheck . <?php echo PJ_INSTALL_URL; ?>index.php?controller=pjFront&amp;action=pjActionCheck How could I find this file(a lot of source files) and maybe place the source code there? 我如何找到此文件(很多源文件),也许将源代码放在那里?

Or should I have to define the new button differently and so I would be able to call my own PHP file at any directory? 还是应该以不同的方式定义新按钮,这样我才能在任何目录中调用自己的PHP文件?

The AJAX URL parameter should be a relative or absolute path to the test.php file? AJAX URL参数应该是test.php文件的相对路径还是绝对路径? Where should I create the test.php file at my directories? 我应该在哪里在目录中创建test.php文件?

Please help me find a quick solution to my issue. 请帮助我快速解决我的问题。

This very easy and simple 这非常容易和简单

1. Create an html page with the form like. 1.创建一个具有如下形式的html页面。

    <form id="sample">
//some thing here....
                <input type="submit" class="btn btn-success" name="submit" value="Create" id="sample">
    </form> 

2. Create an Js page like this. 2.像这样创建一个Js页面。

//click function
          $("#sample").click(function(event) {
              sample();
          });

//ajax function here
          function sample(){
            $.ajax({
                url: '/path/to/file',
                type: 'GET/POST',
                dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
                data: {
                    param1: 'value1'
                },
                success:function(result){
                    alert(result);

                }
            })
            .done(function() {
                console.log("success");
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });

          }

3. Create an php script file. 3.创建一个php脚本文件。

add the actual path in directory like if you are using the localhost example: public/script/test.php then the ajax path is ../script/test.php 在目录中添加实际路径,例如如果您使用本地主机示例:public / script / test.php,则ajax路径为../script/test.php

4. the javascript link to the the html page like 4.指向html页面的javascript链接,例如

example: 例:

<script type="text/javascript" src="sample.js"></script>

您需要在创建表单文件的目录下创建test.php。

If you have a submit-Button the form will be submitted with the action you defined and not with the onclick-event. 如果您有一个Submit-Button,则将使用您定义的操作而不是onclick-event来提交表单。 For that to work you have to simply replace it with <button type="button" 为此,您只需将其替换为<button type="button"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM