1

我遇到的第一个问题是,我想在 index.php 上通过表单向用户提问,并且当按下提交时,它会更新 jcart.php 上的会话变量。使用下面的当前代码,当我稍后调用会话变量时,现在可以找到它,所以我假设我刚才的代码无法正常工作。

第二个问题是当我按下提交时,它会将我带到 jcart.php 是否有办法避免这种情况或让它返回。

在我的 index.php 上,我有一个表格:

<form action="jcart/jcart.php" method="post">
<input type="text" name="example" id="example" />
<input type="submit" name="submit" value="Submit" />
</form>

在 Jcart.php 上:

$_SESSION['example'] = $_POST['example'];

然后在页面上,我在鸡尾酒会.php 上调用它

<?php 
include_once('jcart/jcart.php');
session_start();
?>

<input type="hidden" name="my-item-id" value="<?php echo $_SESSION['example'];?>" />

谢谢你的帮助。

4

3 回答 3

2

There is no need to "update the session variable on jcart.php". Once you store a data into the global $_SESSION array, it should be available on all php files, at least until you destroy the session.

That being said, if jcart/jcart.php needs to have a $_SESSION['example'] variable, you need to be sure, that the session is started before including the file, for instance:

<?php 
    session_start()
    include_once('jcart/jcart.php');
?>

For your other question, you can change the action inside your form to whatever you like or issue a header('Location: /'); to redirect to other page after the value was recieved.

于 2013-05-29T23:01:09.737 回答
1

请试试这个

*jcart.php*

session_start();
$_SESSION['example'] = $_POST['example'];

*then cocktails.php*

include_once('jcart/jcart.php');

echo $_SESSION['example'];
于 2013-05-29T23:02:59.610 回答
0

in jcart/jcart.php

session_start();

should be called at the beginning

于 2013-05-29T23:01:31.533 回答