大家好,我是Web开发的新学习者。
用户点击提交按钮后,我有一个带有表单的页面,然后按钮会将表单内容提交到下一页执行并保存到数据库中。
这样,我的页面将转到另一个页面执行并返回登录页面。
例如:索引.php和执行.php
索引.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>
执行.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.php?(表示将两者组合在一个.php中)这是因为当用户单击提交按钮时,它会转到一个看起来不好看的空白页面来执行。
有人可以帮忙吗?谢谢!=)
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"])会检查它是否被点击吗?
编辑:请忽略这一点,没有完全阅读您的问题
尝试这样的事情:
索引.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>
执行.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>";
}
在你的脑海中添加jQuery lib和你的jQuery perso(myjQuery.js),之后你将用于ajax
索引.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>
留住你的高管.php像这样
执行.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路径有问题。也许你可以纠正一些错误。