PHP和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. So, I am trying to use Jquery and AJAX to make this happen.

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. 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:

<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.

However the code at test.php file is not executed at all. I think that the problem is the form action parameter <?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?

The AJAX URL parameter should be a relative or absolute path to the test.php file? Where should I create the test.php file at my directories?

Please help me find a quick solution to my issue.

3个回答



这非常简单</ p>

1。 使用如下表单创建一个html页面。</ strong> </ p>

 &lt; form id =“sample”&gt; 
//这里有些东西....
&lt; input type =“submit”class =“btn btn-success”name =“submit”value =“Create”id =“sample”&gt;
&lt; / form&gt;

</代码> </ PRE>

<强> 2。 像这样创建一个Js页面。</ strong> </ p>

  // click function 
$(“#sample”)。click(function(event){
sample( );
});

// ajax函数在这里
函数sample(){
$ .ajax({
url:'/ path / to / file',
type:'GET / POST',
dataType:'default:Intelligent Guess(其他值:xml,json,script或html)',
data:{
param1:'value1'
},
成功:function(结果) ){
alert(result);

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

}
</ 代码> </ PRE>

第3。 创建一个php脚本文件。</ strong> </ p>

在目录中添加实际路径,如果您使用的是localhost 示例:public / script / test.php </ strong> 那么ajax路径是 ../ script / test.php </ strong> </ p>

4。 指向html页面的javascript链接</ strong> </ p>

示例:</ p>

 &lt; script type =“text / javascript”  src =“sample.js”&gt;&lt; / script&gt; 
</ code> </ pre>
</ div>

展开原文

原文

This very easy and simple

1. Create an html page with the form like.

    <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.

//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.

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

4. the javascript link to the the html page like

example:

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

dtjkl42086
dtjkl42086 在此处发布您的代码或特定部分,以便我们提供解决方案
2 年多之前 回复
dq05304
dq05304 是的,这正是我在我的应用程序中使用的
2 年多之前 回复
dongsimang4036
dongsimang4036 虽然你的解决方案看起来很合乎逻辑......但直到现在它还没有运作。 click事件的代码似乎根本没有被调用,或者按钮点击事件与Javascript代码不匹配。 你确定按钮的声明应该是那样的吗<input type =“submit”class =“btn btn-success”name =“submit”value =“Create”id =“sample”> ??
2 年多之前 回复



您需要在创建表单文件的同一目录上创建test.php。 </ p>
</ div>

展开原文

原文

you need to create test.php on same directory where your form's file is created.



如果您有一个提交按钮,表单将与您定义的操作一起提交,而不是与onclick-event一起提交。 要使其工作,您只需将其替换为
&lt; button type =“button”</ code> </ p>
</ div>

展开原文

原文

If you have a submit-Button the form will be submitted with the action you defined and not with the onclick-event. For that to work you have to simply replace it with <button type="button"

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐