【问题标题】:How to run execute page in background using ajax? [closed]如何使用ajax在后台运行执行页面? [关闭]
【发布时间】:2013-09-28 02:44:40
【问题描述】:

大家好,我是网络开发的新手。

用户点击提交按钮后,我有一个带有表单的页面,然后按钮会将表单内容提交到下一页执行并保存到数据库中。

这样,我的页面会转到另一个页面执行并返回到着陆页。

例如:index.php 和 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>";
}

我的问题是,如何使用 ajax 在后台运行 exec.php? 谢谢!

【问题讨论】:

标签: php jquery html ajax


【解决方案1】:

正如您标记 jQuery - 只需向 exec.php 文件发送 HTTP 请求:

$('.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';
    });
});

【讨论】:

    猜你喜欢
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多