-1

I have one big problem. My website is made in PHP MVC pattern but I don't use "POST" submit in one page and now I have big problem because I need to pass quantity onto products/order page but if i use Submit button my next page will not work. So I must fix this problem with passing user input field into session variable and get it into another page without using submit button or input button post. I know i must use ajax but when i choose to use localhost/products/order as a page where variable need to pass it doesn't get the variable.

this is my app/views/inc/products/show.php view where i have form which need to make session variable when user change quantity.

I want to use a href button to get into next page but when user click a href it must save input field into session variable. I don't know how to do it. Please help me

    <div class="form-group">
           <label for="proquantity">Product Quantity: </label>
          <input type="text" name="proquantity" id="proquantity" class="form-control form-control-lg" value="">
  

        </div>

        
        <a href="<?php echo URLROOT; ?>/products/order<?php $_SESSION['productId'] = $data['product']->id;?>" class="btn btn-dark">MORE</a>

So my problem is that I don't know how to get this value which is in input with id = "proquantity" and i want that value to pass into this view: app/views/inc/products/order.php

I am not familiar with javascript or jquery so I hope someone will help me to save value which is in input "proquantiy" and save the same value into $_session['proquantity'] so with that I can easily get the value into my order.php view. I can not use sumbit button because i will need to change a lot of staff if i do it now..

What i try? When i try to use this script like this: at the begin of my file of the show.php I add this javascript:

<script type="text/javascript">
$(function(){
   $('#autosend').blur(function(){
       $.post('<?php echo APPROOT;?>/views/inc/products/order.php',{fieldval:$(this).val()},function(response){
          alert(response);
       });
   })
})
        </script>

Then I add this input into same file:

After that I add into my view: app/views/inc/products/order.php

And then I add code to view order.php get that variable like this:

<?php
$_SESSION["proquantity"] = $_POST["fieldval"];
echo $_SESSION["proquantity"];?>

And then i get this error on my order.php page ****Notice: Undefined index: fieldval in C:\xampp\htdocs\shareposts\app\views\products\order.php on line 8 aa


I hope someone will point me to fix my problem and fix this error. I hope i will not need to use submit form.

htaccess of public

    <IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^$ public/ [L]
  RewriteRule (.*) public/$1 [L]
</IfModule>

htaccess of app

Options -Indexes

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Did you do a `session_start();` anywhere in that PHP code? – RiggsFolly Dec 25 '19 at 14:27
  • Yes i have session start in header . And header is included in all files. I put my .htaccess file now in edit, maybe that makes problem? – ajmoprobatopet Dec 25 '19 at 14:33
  • I put again session start and i got this: Notice: session_start(): A session had already been started - ignoring in C:\xampp\htdocs\shareposts\app\views\products\order.php on line 8 – ajmoprobatopet Dec 25 '19 at 14:38

1 Answers1

0

Class

<script>
    $(document).ready(function() {
        $("#orderLink").click(function(event) {
            event.preventDefault();
            var orderLinkHref = $(this).attr('href');
            // Get input field value
            var productQuantity = $("#proquantity").val();
            // Store input field value in session
            sessionStorage.setItem('productQuantity', productQuantity);
            // Redirect Page to the URL
            window.location.href = orderLinkHref;
        })
    });
</script>

<div class="form-group">
    <label for="proquantity">Product Quantity: </label>
    <input type="text" name="proquantity" id="proquantity" class="form-control form-control-lg" value=""/> 
</div>

<a href="/products/order" id='orderLink' class="btn btn-dark">MORE</a>

Try storing input field value like this in session storage.

prathameshk73
  • 1,048
  • 1
  • 5
  • 16
  • I think the OP wants to use the server session and not the browsers – RiggsFolly Dec 25 '19 at 14:45
  • I just want to populate the Session with this variable of input field into order.php I don't care if that is server side or client side but on order.php must get into $_SESSION – ajmoprobatopet Dec 25 '19 at 14:48