0

I'm trying to pass a value which is input into a box on one Php page (itinerary.php) into another Php page ('submit.php') so it can, from there, be saved into a database. But I can't quite figure out how to get it across. I've tried using GET as you can see from code below, but I am already using a GET statement to receive and acknowledge another value from that very same page 'submit'. I guess I am overcomplicating it, but my knowledge of Php is still pretty limited at this stage so any ideas would be appreciated!

This is an extract from the itinerary.php file (it sits within a Bootstrap/Html framework. Note the entry which contains the input box for the sequence number).

<h3><br>YOUR ITINERARY</h3>

<?php

//Display contents of itinerary
    if(!empty($_SESSION['itinerary'])){

        //Retrieve details of each location in array from database
        $query = "SELECT * FROM locations WHERE loc_id IN (";
        foreach ($_SESSION['itinerary'] as $loc_id=>$value)
        {$query.=$loc_id.',';}
        $query = substr($query, 0, -1).')ORDER BY loc_id ASC';
        $result = mysqli_query($db, $query);

        echo'<table><tr><th colspan="5">LOCATIONS IN YOUR ITINERARY</th></tr>';

    //Display locations in array

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

    $loc_id = $row['loc_id'];

        echo"<br><td>{$row['loc_name']}</td></tr><br>
        <td>{$row['loc_desc']}</td>
        <td><p>Seq. No</p><input type=\"text\" size=\"3\"  name=\"sequence\" value=????????>NOT SURE WHAT TO ADD INTO THIS LINE TO RETAIN THE INFO THAT IS INPUT</td>
        <td><a href=remove_loc.php?value=$loc_id>Remove location</a><br></br></td>
    </tr><br></table>";
    }//while
    print_r($_SESSION);

    mysqli_close($db);

    }//if

    else {echo '<p><br>Your itinerary is empty.<br></p>';}

echo '<br><p>

<a href=submit.php?submit>Save itinerary</a>
<a href=clear_itin.php?clear>Clear itinerary</a>
<a href="user_home.php">Your details</a>
<a href="logout.php">Logout</a>
</p>';


?>

And this is where I am trying to receive it and then use it in a SQL command to add to the database. (Again, this is within a Bootstrap framework)You can ignore the first SQL Insert statement as it is passing other info in successfully anyway..

<div class="row">
        <div class="col-md-9">

<?php

if (isset($_GET['sequence'])){
    $sequence = $_GET['sequence'];

}//if

    if (isset($_GET['submit'])){

        $query = "INSERT INTO itineraries (user_id, date_created) VALUES (".$_SESSION['user_id'].", NOW())";
        $result = mysqli_query($db, $query);

        $itinerary_id = mysqli_insert_id($db);

        $retrieve_locs = "SELECT * FROM locations WHERE loc_id IN (";
        foreach ($_SESSION['itinerary'] as $id=>$value)
        {$retrieve_locs.=$id.',';}
        $retrieve_locs = substr($retrieve_locs, 0, -1).')ORDER BY loc_id ASC';
        $result = mysqli_query($db, $retrieve_locs);

        //Store items in itin_locs db
        while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC)){

            //This is the command I have been trying to use, commented out. The second one below this works fine, but obv doesn't input a sequence number.

            //$insert_locs = "INSERT INTO itin_loc (itinerary_id, loc_id, sequenceNo) VALUES ($itinerary_id, ".$row['loc_id'].", $sequence)";

            $insert_locs = "INSERT INTO itin_loc (itinerary_id, loc_id) VALUES ($itinerary_id, ".$row['loc_id'].")";
            $insert_result = mysqli_query($db, $insert_locs);

            echo mysqli_error($db);

            if ($insert_result === FALSE) {
                die("Query failed!" . mysql_error() . $insert_locs);}


        }//while


        mysqli_close($db);

        echo"<p>Your itinerary is saved!. Itinerary number is #".$itinerary_id."</p><br>";
        $_SESSION['itinerary']= NULL;

        echo '<p>

<a href="user_home.php">Your details</a>
<a href="logout.php">Logout</a>
</p>';

    }//if


    else {echo '<p>Your itinerary is empty.<br></br></p>';}

?>
ThomasM
  • 37
  • 3
  • You need some storage. May be [sessions](https://secure.php.net/manual/en/book.session.php). – vp_arth Jun 28 '16 at 10:42
  • 1
    Possible duplicate of [PHP Pass variable to next page](http://stackoverflow.com/questions/871858/php-pass-variable-to-next-page) – FirstOne Jun 28 '16 at 10:43
  • you can use different get parameters: `?p1=param1&p2=param2&p3=param3` and so on, later you can receive them with `$_GET['p1']` and so on... So basically instead of using ?submit, you should do ?action=submit Also if you use form with method POST, you could disable option to refresh the page with URL 100 times read about forms here: http://www.w3schools.com/tags/tag_form.asp – inubs Jun 28 '16 at 10:44
  • Yes, all this is taking place within sessions - the user is logged in, adds locations to their itinerary and then adds a sequence number to each location within that itinerary, before saving it to database. – ThomasM Jun 28 '16 at 10:47
  • Not sure why you're over complicating this. It looks like a perfect case scenario for html `form` just like @MaanusIndov said. – Michal Bieda Jun 28 '16 at 10:51
  • Yes @ThomasM, save yourself alot of time and trouble, just create a simple form with some inputs (type=text) or even some hidden ones if needed (type=hidden). set `
    – inubs Jun 28 '16 at 10:58

1 Answers1

1

Use a form on your HTML page to hold all the input fields in the table. Also instead of anchor "a" tag use a submit button to submit the form to other page. Use some thing like:

Send value of submit button when form gets posted

Community
  • 1
  • 1
Sharry India
  • 341
  • 1
  • 9