$_GET to pass a variable

$_GET to pass a variable

roadjanroadjan Posts: 22Questions: 6Answers: 0
edited November 2021 in Editor

My code works great with a fixed number but my PHP code won't get a variable from $_GET or $_COOKIE - can anybody help?

$customer_id = 277; <<-- this is where I want to pass $_GET or $_COOKIE data but it will not work...

This question has an accepted answers - jump to answer

Answers

  • roadjanroadjan Posts: 22Questions: 6Answers: 0

    ```
    <?php

    $customer_id = 277;

    /*
    * Example PHP implementation used for the index.html example
    */

    // DataTables PHP library
    include( "../datatables/DataTables.php" );

    // Alias Editor classes so they are easy to use
    use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

    // Build our Editor instance and process the data coming from _POST
    Editor::inst( $db, 'ship_to', 'ship_to_id')
    ->fields(
    Field::inst( 'ship_to_customer_id' )->setvalue($customer_id),
    Field::inst( 'ship_to_delivery_address_1' ),
    Field::inst( 'ship_to_id' ),
    Field::inst( 'ship_to_delivery_address_2' ),
    Field::inst( 'ship_to_delivery_town' ),
    Field::inst( 'ship_to_delivery_county' ),
    Field::inst( 'ship_to_delivery_postcode' )
    )

    ->where('ship_to_customer_id', $customer_id)
    
    ->debug(true)
    ->process( $_POST )
    ->json();
    
    <?php > ``` ?>
  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    How are you trying to get the customer_id parameter?

    How are you passing the parameter?

    Kevin

  • roadjanroadjan Posts: 22Questions: 6Answers: 0

    I have it in the URL of the page calling the shipto.php file using serverside on the rendered page, and I am also storing it in a cookie - both called 'customer_id'.

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    This W3Schools $_GET example shows how to pass variables in the URL and to use $_GET to get them. Start by using the browser's network inspector to make the URL is formatted as expected.. If you still need help please post your code or better a link to your page or a test case replicating the issue so we can offer suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • roadjanroadjan Posts: 22Questions: 6Answers: 0

    I am familiar with how $_GET works. My problem is thet when I define it in the server side php file it does not read it in. Nor when I read the cookie ($_COOKIE). If I hard code an ID it works perfectly, but I need a dynamic variable for 'customer_id' to be passed in.

    $customer_id = $_GET['customer_id'] // does not work
    $customer_id = $_COOKIE['customer_id'] // does not work
    
    $customer_id = 123 // does work
    
  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    edited November 2021

    I see this in your script:

    process( $_POST )

    Sounds like you are using an HTTP POST instead of GET. See this example of sending parameters along with this example of POST requests.

    gain if this doesn't help please post your client side code so we can see what you are doing. Or better a link to your page or test case.

    Kevin

  • roadjanroadjan Posts: 22Questions: 6Answers: 0

    OK, I think I made progress... I am now editing the editor .js file and am now getting data in the url for datatables and for editor. The URL in inspector is respecting the change in URL and the $_GET is now working in the editor php file... can you offer any advice as to how I get a PHP variable into the url string in the ajax url?

  • roadjanroadjan Posts: 22Questions: 6Answers: 0

    The URL in inspector now shows these URL's, so now all I have to do is...

    In the editor ajax section I want to replace the 277 with a php variable

            "ajax": "../php/shipto.php?customer_id=277",
            "table": "#shipToAddress",
    

    In the datatables url section I want to replace the 277 with a php variable

            ajax: {
                url: "../php/shipto.php?customer_id=277",
                type: "POST"
            },
    
  • roadjanroadjan Posts: 22Questions: 6Answers: 0

    Thanks for your help so far Kevin, I can not share the page as it is secure. Everything is working great, I just need to get a PHP variable into thos URL's in editor and datatables ajax areas.

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Did you see the example of sending parameters link I posted above?

    Kevin

  • roadjanroadjan Posts: 22Questions: 6Answers: 0

    I did, yes. Thanks Kevin. This is getting lost in translation. I just need to pass a single variable into the js file, in both datatabkes and editor. It is all working just as it should, barring the use of the correct variable. If I hard code it, it works, so how why do I need to reading up on GET and POST requests. I am sorry, but I am obviously not being clear enough. I don't know how else to phrase my problem - it seems pretty clear. I have already progressed just through trial and error...

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Sorry I posted the wrong link. See this example of sending parameters.

    Kevin

  • roadjanroadjan Posts: 22Questions: 6Answers: 0

    I have sorted it. On the page calling the js script for datatables / editor I declared the variable in javascript

    <script>var customer_id = "<?= $customer_id ?>";</script>
    <script src="../js/table.shipto.js"></script>
    

    Then I called that variable in both ajax locations like this

    "ajax": "../php/shipto.php?customer_id=" + customer_id + "",
    

    and like this

    url: "../php/shipto.php?customer_id=" + customer_id + "",
    

    No changes to POST / GET, just getting the syntax / method correct for passing a simple variable...

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951
    Answer ✓

    url: "../php/shipto.php?customer_id=" + customer_id + "",

    Doing this probably won't work if the customer_id changes. It will probably be statically assigned to whatever the value is when initialized. Taht is why you need to use the ajax.data option as a function like the example.

    Kevin

Sign In or Register to comment.