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.

    打赏 评论
  • dsbiw2911188 2013-07-31 14:27

    The reason why it doesn't work is that you are using sessions wrong.

    The workflow of application should be like:

    1. user enters the amount
    2. you process it, if needed you give an error message or something else
    3. save amount to session
    4. redirect user to next page
    5. calculate price
    6. display result

    Code example using your code:

    Page1 should be:

    <?php
    session_start();
    if(isset($_GET['wart1'])) {
        $_SESSION['towar1'] = (int) $_GET['wart1'];
        header('Location: page2.php');
    }
    ?>
    
    <form method="get" action="kontakt.php">
    <input id="wart1" type="number" value="0" name="towar1" class="ilosc" required>
    </form>
    

    Page2:

    <?php
    session_start();
    $cena1 = $_SESSION['wart1'] * 19;
    ?>
    
    <label for="wiadomosc"></label>
    <textarea placeholder="" name="wiadomosc" required><?php echo $cena1 ?></textarea>
    
    打赏 评论

相关推荐 更多相似问题