doudi8519 2013-07-31 14:15 采纳率: 100%
浏览 57
已采纳

使用PHP会话将值传输到另一个页面 - textarea

Transfer value to another page was successful, I have read many topics on this subject, stockoverflow really helped me with this.

On the first page (eg page1.php) I have input. Normally I type a value to it.

With php I write in a session variable and after clicking submit I can transfer this value to the other side (eg page2.php) which is a simple contact page. This value is pasted into a textarea field.

My problem is that it takes only the value that I type directly to the input, which is located in page1.php. I wanted to create rather calculator, which will consist of the price according to the entered number of kilograms of product. (the kilogram is the value entered in the input).

How do I get a form field was clean, and the Order the product was all redirected correctly?

My code in page1.php :

<?php
session_start();
$_SESSION['towar1'] = $cena1;
?>

<form method="get" action="kontakt.php">
<input id="wart1" type="number" value="0" name="towar1" class="ilosc" required>
</form>

My code in page2.php(conttact page) :

<?php
session_start();
$cena1 = $_GET['towar1'];
?>

<label for="wiadomosc"></label>
<textarea placeholder="" name="wiadomosc" required><?php echo $cena1 ?></textarea>

///PROBLEM

when I trying to figure how to get good value of product price (e.g. for 1 kg will be 19$) i wrote code of this kind directly on my page2.php:

<?php echo $cena1*19 ?>

But when I do this, any direct entrance on the contact page gives me already written text which is 0 (zero).

Can I avoid that 0? How can I make textarea will be blank always, and my page1.php (calculator) will calculate correct number (const which is 19$ per kilo and number of kilos that you entered in input)

  • 写回答

2条回答 默认 最新

  • doudui1850 2013-07-31 14:25
    关注

    Your question is quite not clear, lets go through this: page1.php contains input that manage Item quantity and $_SESSION variable with its price, on the page 2 you need to display quantity*price_per_item number, right? If yes, try the following approach:

    <?php
    session_start();
    $cena1 = $_GET['towar1'] * $_SESSION['towar1'];
    ?>
    
    <label for="wiadomosc"></label>
    <textarea placeholder="" name="wiadomosc" required><?php echo $cena1 ?></textarea>
    

    , where $_GET['towar1'] is an items quantity and $_SESSION['towar1'] is an item price.

    p.s For more adnvanced technique do not hesitate to ask in comments.

    Hope it helps, cheers.

    EDIT

    Considering our converstation in comments I can assume that this approach would helps you:

    $cena1 = (!empty($_GET['towar1']) && $_GET['towar1'] != 0 )? $_GET['towar1'] * 19 : '';

    This line check if $_GET['towar1'] variable is not empty and not zero. If it match the condition - make calculation, else - return an empty string (not zero).

    Please note that everything we're talking about above is not secure and can be abused by end-user. Please consider to review input validation aproach.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致