doudi8519 2013-07-31 06: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 06: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部