将数据添加到按钮上的SQL点击
问题描述:
我的页面接收到数据,我用$ _post检索。我显示一些数据,在页面的底部,我的按钮必须将数据保存到mysql。我可以将表单提交到下一页,但是如何访问我使用后检索的数据呢?可以说我有下面的代码(在现实中很多更多的变数。):将数据添加到按钮上的SQL点击
<?php
$v= $_POST["something"];
echo $v;
echo "Is the following information correct? //this would be at the bottom of the page with the buttons
?>
<input type="button" value="submit data" name="addtosql">
答
您可以通过两种方法做到这一点:
1)您可以保存POST
变量在hidden
场。
<input type="hidden" name="somevalue" value="<?php if(isset($_POST["something"])) echo $_POST["something"];?>" >
隐藏的值也会被传递到FORM
提交的操作页面。在该页面,您可以访问使用
echo $_POST['somevalue'];
2)该值使用SESSION
您可以将值存储在SESSION
,可以在任何其他网页访问。
$v= $_POST["something"];
session_start();
$_SESSION['somevalue']=$v;
并在接下来的页面访问使用SESSION
变量,
session_start();
if(isset($_SESSION['somevalue']))
echo $_SESSION['somevalue'];
答
看看。下面的每件事情应该在单个PHP页面上
// first create a function
function getValue($key){
if(isset($_POST[$key]))
return $_POST[$key];
else
return "";
}
// process your form here
if(isset($_POST['first_name']){
// do your sql stuff here.
}
// now in html
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="first_name" value="<?php echo getValue("first_name"); ?>" />
<input type="submit" />
</form>
+0
是的,这将工作,但我有100个变量。我不能为每一个做这个 – Newb 2014-09-25 10:53:09
我有100个变量要保存。有没有更好的方法? – Newb 2014-09-25 10:38:42
@ Newb.No then use SESSIONS – Jenz 2014-09-25 10:39:39
是否有可能创建所有数据的临时表,然后在下一页将其转移到真正的表? – Newb 2014-09-25 11:00:03