Php 如何使按钮在同一页而不是下一页上执行
大家好,我是网络开发的新手 我有一个带有表单的页面,用户单击“提交”按钮后,该按钮将表单内容提交到下一个页面执行并保存到数据库中 因此,我的页面将转到另一个页面执行并返回到登录页面 例如:index.php和exec.php index.php:Php 如何使按钮在同一页而不是下一页上执行,php,jquery,html,mysql,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
<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>";
}
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>";
}
if($\u POST[“g-product”]&&&$\u POST[“g-reason”]!=“”)
{
$gpproduct=$_POST[“g-product”];
$greason=$_POST[“g-reason”];
$insert=“insert INTO jovine.vote(投票编号、产品名称、原因、类型)值(NULL、$GPPRODUCT’、$GLASON、$goat”);
$result=mysql\u查询($insert,$con);
回声“;
echo“提醒('谢谢。您的投票已被记录在案');”;
echo“window.location.href='index.php';”;
回声“;
}
我的问题是,如何在index.php中执行submit按钮而不转到exec.php?(表示在a.php中结合两者)
这是因为当用户单击submit按钮时,它会转到一个空白页来执行,这看起来并不好看
有人能帮忙吗?谢谢
=)编辑:请忽略此问题,不要像看上去那样完整地阅读您的问题 试着这样做: 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';";
echo "</script>";
}
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>";
}
if($\u POST[“g-product”]&&&$\u POST[“g-reason”]!=“”)
{
$gpproduct=$_POST[“g-product”];
$greason=$_POST[“g-reason”];
$insert=“insert INTO jovine.vote(投票编号、产品名称、原因、类型)值(NULL、$GPPRODUCT’、$GLASON、$goat”);
$result=mysql\u查询($insert,$con);
回声“;
echo“提醒('谢谢。您的投票已被记录在案');”;
echo“window.location.href='index.php?hidesubmit=1';”;
回声“;
}
if(isset($\u POST[“g-btn”]))
{
$gpproduct=$_POST[“g-product”];
$greason=$_POST[“g-reason”];
$insert=“insert INTO jovine.vote(投票编号、产品名称、原因、类型)值(NULL、$GPPRODUCT’、$GLASON、$goat”);
$result=mysql\u查询($insert,$con);
回声“;
echo“提醒('谢谢。您的投票已被记录在案');”;
echo“window.location.href='index.php';”;
回声“;
}
你为什么喜欢它
你可以这样做。。。isset($_POST[“g-btn”])将检查它是否被点击?在您的头脑中添加jQuery库,并在之后用于ajax的jQuery人员(myjQuery.js) index.php
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="/js/myjQuery.js"></script>
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>";
}
您可以试试这段代码,也许您的文件的js路径或php路径有问题。还有一些可以纠正的错误。使用ajax方法在后台执行exec.php,并使用jquery在index.phptry中检索结果ajax@DonovanCharpin我可以知道我该怎么写剧本吗?我有点不明白莫纳尔=(它应该可以工作,但您需要一个不发送表单的解决方案-我的解决方案发送表单并使用特殊参数重定向到原始页面。根据此参数,是否显示提交按钮。
$(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');
}
});
});
});