如何使按钮在同一页面上执行,而不是在下一页上执行

时间:2013-09-23 07:55:52

标签: php jquery html mysql

大家好,我是一名新的网络开发学习者。

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

这样,我的页面将转到另一个页面执行并返回到登录页面。

例如: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>";
}

我的问题是,如何在不进入exec.php的情况下执行index.php中的提交按钮? (意味着将两者结合起来.php) 这是因为当用户点击提交按钮时,它会转到一个空白页面来执行,这看起来不太好。

任何人都可以帮忙吗?谢谢! =)

3 个答案:

答案 0 :(得分:0)

编辑:请忽略这一点,完全看不到你的问题

尝试这样的事情:

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

    <?php if(!isset($_GET['hidesubmit'])): ?>
    <input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;">
    <?php endif; ?>
</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?hidesubmit=1';";
    echo "</script>";
}

答案 1 :(得分:0)

if (isset($_POST["g-btn"]))
{
 $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>";
}

<html>
<form name="g-form" action="a.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>
</html>

你可以这样做... isset($ _ POST [“g-btn”])会检查它是否被点击?

答案 2 :(得分:0)

添加jQuery lib和你将用于ajax的jQuery perso(myjQuery.js)

<强>的index.php

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="/js/myjQuery.js"></script>

在表单上创建表单而不采取任何操作。

<html>
    <form name="g-form" class="goat-vote">
        <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" value="vote" style="margin-left:470px; cursor:pointer;">
    </form>
</html>

保持你的exec.php像这样

<强> 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?hidesubmit=1';";
    echo "</script>";
}

并创建myjQuery.js

<强> myjQuery.js

$(document).ready(function(){
    $(".vote-btn").on("click", function(){
        g-product = $("input[name='g-product']").val();
        g-reason = $("input[name='g-reason']").val();
        $.ajax({cache:false,dataType:'html',crossDomain:true,type:'POST',
            url:'/php/exec.php',data : {g-product:g-product, g-reason:g-reason},
            success:function(html){
                console.log(html);
            },
            error:function(j,t,e){
                console.log('problem');
            }
        });
    });
});

你可以尝试这个代码,也许你的文件的js路径或php路径有问题。也许你可以纠正一些错误。