繁体   English   中英

通过带有 href 的 id 链接获取价值

[英]get value by id link with the href

这是我的输入

<input type="text" name="quantity" id="quantity" value="1" />

这是我的href链接

<a href="UserOrderProduct.php?id=<?php echo $Pid; ?>
&price=<?php echo $Price; ?>&quantity=">add</a>

如何写在href中,请忽略

<?php echo $Pid; ?>&price=<?php echo $Price; ?>

这是因为我已经从数据库中获得了 id 但只有数量而不是真正如何获得它,可以教我吗?

如果您有一个表单,您可以从代码中获得“数量”值:

<input type="text" name="quantity" id="quantity" value="1" />

如果您使用“GET”方法

<FORM method='get' action='next_script.php'>

所有值都将在地址中,否则,如果您发布值,您将通过 $_POST 数组在下一个脚本中获取它们。

如果您需要在不同脚本之间传递一对或多对变量=值,最好的解决方案是使用 PHP 会话。

您只需要在每个脚本的开头添加:

session_start();

并将 your_variable 和 your_value 设置为:

$_SESSION['your_variable'] = your_value;

或者

$_SESSION['session_variable'] = $your_variable;

这样,数据在链接上不可见,您可以在任何您喜欢的地方访问它们,前提是您将 session_start() 命令保留在每个 php 页面的开头。

如果您有 html...

<form method="post" action="phpfile.php"> 
<input type="text" name="quantity" id="quantity" value="1" />
<input type="submit" value="send">
</form>

...在您的 phpfile.php 中,您可以像这样检索数量的值:

<?php
$qty = $_POST['quantity'];
?>
<a href="UserOrderProduct.php?id=<?php echo $Pid; ?>
&price=<?php echo $Price; ?>&quantity=<?php echo $qty;?>">add</a>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM