Javascript JQUERY/AJAX请求未通过和冲突
我已经在我的页面中包含了一个联系人表单。在同一页中,我有一个脚本,它根据下拉列表的值获取价格。现在,当我试图提交联系信息时,我与脚本的价格有冲突。基本上,它试图运行它,我不知道为什么。此外,提交时的联系人表单也不起作用…我只需要打开一个新页面,其中包含URL..?message=blabla 知道出了什么问题吗 我正在开发Laravel4.2,因此您看到的路径将重定向到我的php函数。 下面是代码,下面是php代码:Javascript JQUERY/AJAX请求未通过和冲突,javascript,php,jquery,ajax,laravel,Javascript,Php,Jquery,Ajax,Laravel,我已经在我的页面中包含了一个联系人表单。在同一页中,我有一个脚本,它根据下拉列表的值获取价格。现在,当我试图提交联系信息时,我与脚本的价格有冲突。基本上,它试图运行它,我不知道为什么。此外,提交时的联系人表单也不起作用…我只需要打开一个新页面,其中包含URL..?message=blabla 知道出了什么问题吗 我正在开发Laravel4.2,因此您看到的路径将重定向到我的php函数。 下面是代码,下面是php代码: public function postSendMessage() {
public function postSendMessage() {
echo "<span class=\"alert alert-success\" >Your message has been received. Thanks!</span><br><br>";
}
公共函数postSendMessage(){
echo“您的邮件已收到。谢谢!
”;
}
取消单击,以便表单不会提交
$("button#send").click( function(evt){
evt.preventDefault();
新错误,窗体具有联系人id,而不是类
data: $('form.contact').serialize(),
需要
data: $('form#contact').serialize(),
这就是我在同样情况下所做的
//For your drpbox use this code
$(document).on("change", "#yorDropBoxId", function(){
dropBoxValue=$("#yorDropBoxId").val();
var request = $.ajax({//http://api.jquery.com/jQuery.ajax/
url: "samePagePHPcript.php",
type: "POST",
data: {
ObjEvn:"dropBoxEvent",
dropBoxValue: dropBoxValue //You will use $myVar=$_POST["dropBoxValue"] to retrieve the information from javascript
},
dataType: "json"
});
request.done(function(dataset){
//If you want to retrieve information from PHP sent by JSON.
for (var index in dataset){
JsResponse=dataset[index].phpResponse;
}
if(JsResponse test someting){
"do dometing"
control the beheaivor of your HTML elements
}
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
});
//To submit your form use this code. You must use Prevent default if you are using a button or using a <a> link tag to trigger the evenrmrnt
$(document).on("click", "#btn_sendForm", function(e){
e.preventDefault();
var dt={
ObjEvn:"FormEvent",
input1:$("#txt_input1").val(),
input2: $("#txt_input2").val(),
input3: $("#txt_input3").val()
};
var request = $.ajax({//http://api.jquery.com/jQuery.ajax/
url: "samePagePHPcript.php",
type: "POST",
data: dt,
dataType: "json"
});
request.done(function(dataset){
//If you want to retrieve information from PHP send by JSON.
for (var index in dataset){
JsResponse=dataset[index].phpResponse;
}
if(JsResponse test someting){
"do dometing"
control the beheaivor of your HTML elements
}
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
});
//In the samePagePHPcript.php you can do this:You will return your information from PHP using json like this
$event = $_POST["ObjEvn"];
if(event==="FormEvent"){//Event to insert in your form
$arrToJSON = array(
"phpResponse"=>"data you want to send to javascript",
"asYouWant"=>"<div class=\".class1\">more data</div>"
);
echo json_encode(array($arrToJSON));
}
elseif(event==="dropBoxEvent"){//Event to your dropbox - if you want
$arrToJSON = array(
"phpResponse"=>"data you want to send to javascript",
"asYouWant"=>"<div class=\".class1\">more data</div>"
);
echo json_encode(array($arrToJSON));
}
//对于您的drpbox,请使用此代码
$(document).on(“change”、“#yorDropBoxId”,function(){
dropBoxValue=$(“#yorDropBoxId”).val();
var请求=$.ajax({//http://api.jquery.com/jQuery.ajax/
url:“samepagephpccript.php”,
类型:“POST”,
数据:{
对象:“dropBoxEvent”,
dropBoxValue:dropBoxValue//您将使用$myVar=$\u POST[“dropBoxValue”]从javascript检索信息
},
数据类型:“json”
});
request.done(函数(数据集){
//如果您想从JSON发送的PHP中检索信息。
对于(数据集中的var索引){
JsResponse=dataset[index].phpResponse;
}
如果(JsResponse test someting){
“做圆顶”
控制HTML元素的beheavor
}
});
请求失败(函数(jqXHR,textStatus){
警报(“请求失败:+textStatus”);
});
});
//要提交表单,请使用此代码。如果使用按钮或链接标记触发evernmrnt,则必须使用“防止默认值”
$(文档)。在“单击”上,函数(e){
e、 预防默认值();
var dt={
对象:“FormEvent”,
input1:$(“#txt_input1”).val(),
input2:$(“#txt_input2”).val(),
input3:$(“#txt_input3”).val()
};
var请求=$.ajax({//http://api.jquery.com/jQuery.ajax/
url:“samepagephpccript.php”,
类型:“POST”,
数据:dt,
数据类型:“json”
});
request.done(函数(数据集){
//如果要从PHP检索信息,请通过JSON发送。
对于(数据集中的var索引){
JsResponse=dataset[index].phpResponse;
}
如果(JsResponse test someting){
“做圆顶”
控制HTML元素的beheavor
}
});
请求失败(函数(jqXHR,textStatus){
警报(“请求失败:+textStatus”);
});
});
//在samepagephpccript.php中,您可以这样做:您将使用json从php返回信息,如下所示
$event=$_POST[“ObjEvn”];
如果(event==“FormEvent”){//要在表单中插入的事件
$arrToJSON=array(
“phpResponse”=>“要发送到javascript的数据”,
“asYouWant”=>“更多数据”
);
echo json_编码(数组($arrToJSON));
}
elseif(event===“dropBoxEvent”){//event到您的dropbox-如果需要
$arrToJSON=array(
“phpResponse”=>“要发送到javascript的数据”,
“asYouWant”=>“更多数据”
);
echo json_编码(数组($arrToJSON));
}
您不会取消表单提交。如果我是对的,请不要提交。。。仅输入一个提交type@epascarello,不太明白你的意思?表格必须在单击后提交。冲突在于获取名为retrievePrice的价格的脚本。感谢您在单击时调用Ajax,那么您为什么需要提交表单呢?尝试添加代码。@AndreasFursterSend
这是一个提交按钮。哦……我现在明白你的意思了。所以我可以删除按钮的提交类型?现在我也收到了此错误,但名称是正确的:0未捕获的语法错误:数据的意外标识符:$('form.contact')。serialize(),第一个dropbox函数的确切用途是什么?为什么脚本现在这么长?我在一个多语言网站上使用这个脚本。你有两个函数。一个用于下拉框,另一个用于按钮发送表单。你可以简化它,一次就搞定它。我的网站非常动态。所以我的dropbox可以发送消息给serever检索信息并做一些事情。形成相同的想法。我与这种技术没有冲突。即使我调用同一个php页面来分析触发了哪个事件