Search code examples
phpformssessionisset

Passing value of Input text field into Session Variable


I used method GET to get the value inside the input text field.

<?php 
echo "<form method='GET'>"; 
echo "<input type='text' placeholder='0' name='valueinputed' value ='$value'?>";
echo '<a href="mycart.php?valueinput='.$idsecure.'">Submit </a>';
echo "</form>";
?> 

where when the submit clicked it will get the product id and its session variable.

and go page mycart

if(isset($_GET['valueinput'])){
$valueinputed = $_GET['valueinputed'];
$_SESSION['cart_'.(int)$_GET['valueinput']] = $valueinputed;

header('location:'.$page);

}

This code is setting input text field and set the session variable. I used this code to show to you what I mean and I want to do. I also used post method but it resulted me to delete the entire session variable(specifically).

Anyway, my variable for quantity of product is $value and for each this way

thank you for reading this, I really appreciate any help.


Solution

  • <a href="mycart.php?valueinput='.$idsecure.'">Submit </a> doesn't submit the form, it just links to the page in the href.

    Change your form to

    <?php 
    echo "<form method='GET' action='mycart.php'>"; 
    echo "<input type='hidden' name='valueinput' value='$idsecure'>";
    echo "<input type='text' placeholder='0' name='valueinputed' value='$value'>";
    echo '<input type="submit" name="submit">';
    echo "</form>";
    ?>
    

    to submit the form with all of its fields to mycart.php, where you can then access $_GET['valueinputed'].