如何使用Ajax在后台运行执行页面? [英] How to run execute page in background using ajax?
问题描述
在用户点击提交按钮后,我有一个带有表单的页面,然后按钮将提交表单内容到下一页要执行并保存到数据库中。
这样,我的页面将转到另一个页面执行并返回到登录页面。
例如:index.php和exec.php
index.php:
< form name =g-formaction =gbtn-exec.phpmethod =postclass =goat-voteonsubmit =return validategForm() >
< input type =textname =g-productplaceholder =Brand / Product Namestyle = - moz-border-radius:5px; border-radius:5px; padding-left:20px ; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg'; color:#000; font:1.6em; />
< p class =g-question>为什么喜欢它?< / p>
< textarea name =g-reasonstyle = - moz-border-radius:5px; border-radius:5px; padding:5px; opacity:.5; border:none; margin -left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg'; color:#333; font-size:1em;>< / textarea>
< input name =g-btnclass =vote-btntype =submitvalue =votestyle =margin-left:470px; cursor:pointer;> ;< /形式>
exec.php
if($ _POST [g-product]&& $ _POST [g-reason]!=)
{
$ gproduct = $ _POST [ G-产物];
$ greason = $ _POST [g-reason];
$ insert =插入jovine.vote(vote_id,product_name,reason,type)VALUES(NULL,'$ gproduct','$ greason','goat');
$ result = mysql_query($ insert,$ con);
echo< script>;
echoalert('谢谢。你的投票已被记录。');;
echowindow.location.href ='index.php';;
echo< / script>;
$ / code>
我的问题是,如何使用ajax在后台运行exec.php ?
Thanks!
当您标记jQuery时 - 只需发送一个HTTP请求到 exec .php
file:
$('。vote-btn')。on('click' ,函数(){
$ .ajax({
url:exec.php,
data:{g-product:$('#g-product')。val() ,g-reason:$('#g-reasons')。val()}
})。done(function(){
alert('谢谢。你的投票已被记录。') ;
window.location.href ='index.php';
});
});
Hi all Im a new learner for web development.
I have a page with a form after user click the submit button, then button will submit the form content to the next page to execute and save into database.
So that, my page will go to another page to execute and return to the landing page.
Eg: index.php and exec.php
index.php:
<form name="g-form" action="gbtn-exec.php" method="post" class="goat-vote" onsubmit="return validategForm()">
<input type="text" name="g-product" placeholder="Brand / Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" />
<p class="g-question">Why you love it?</p>
<textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea>
<input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;"></form>
exec.php
if ($_POST["g-product"] && $_POST["g-reason"] != "" )
{
$gproduct = $_POST["g-product"];
$greason = $_POST["g-reason"];
$insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')";
$result = mysql_query($insert,$con);
echo "<script>";
echo "alert('Thank you. Your vote has been recorded.');";
echo "window.location.href='index.php';";
echo "</script>";
}
My question is, how do I use ajax to run exec.php in background? Thanks!
As you tagged jQuery - simply send a HTTP request to the exec.php
file:
$('.vote-btn').on('click', function() {
$.ajax({
url: "exec.php",
data: { g-product: $('#g-product').val(), g-reason: $('#g-reasons').val() }
}).done(function() {
alert('Thank you. Your vote has been recorded.');
window.location.href='index.php';
});
});
这篇关于如何使用Ajax在后台运行执行页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!