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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!