Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

php POST GET

I have a relatively simple question (I hope):

I have the following code in my php page:

Section 1:
-------------
<?php

$query = "SELECT * FROM orders WHERE agnt = 'CHARLES BENSON'";
      
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
      $qstr .= "&ID[".$row [ID]."]";
      
}
?>

Section 2:
-------------

<a href="tcpdfOutput.php?<?php echo $qstr; ?>"><strong>Save To PDF:</strong></a>

 This code is working fine at the moment. Section 1 takes records from a recordset and creates an ID string which is sent to the url in Section 2
As I say, all works perfectly EXCEPT when the ID string gets too long and the xampp server refuses to handle it.
Question: Is it possible to convert the string from GET (as it is now) to POST (obviously the downstream pages will be changed accordingly)?
0
doctorbill
Asked:
doctorbill
  • 4
  • 4
  • 3
4 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
A link  (<a href...) is be definition a GET and you can not make it a POST.  POST has to come from a form, an AJAX request or direct thru something like 'curl'.  I think you need to rearrange your code so that the link identifies the database request that needs to be made instead of getting all the data and trying to put it in the link.

Pass a simpe identifier, make the database request, and then pass it to the PDF generator.  Makes for a lot less network traffic too.
0
 
doctorbillTechAuthor Commented:
can you give me an example of an identifier I could use please
0
 
Dave BaldwinFixer of ProblemsCommented:
You will have to create the code to go with this.  It looks like your key to the data in your example is "CHARLES BENSON".  If that is in a variable named $agnt, then you would use that as your query.

In your original PHP:
$agnt = urlencode($agnt);  // to make sure it gets passed correctly
$qstr .= "&ID=$agnt]";

In your HTML/PHP;
<a href="tcpdfOutput.php?<?php echo $qstr; ?>"><strong>Save To PDF:</strong></a>

Then the database query becomes the first thing in 'tcpdfOutput.php'.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ray PaseurCommented:
I would probably move the DB query to the PDF creator.  Then you could make a URL like this:

<?php
$qstr = urlencode('Charles Benson');
?>
<a href="tcpdfOutput.php?<?php echo $qstr; ?>"><strong>Save To PDF:</strong></a>
0
 
doctorbillTechAuthor Commented:
The problem is that the ID's need to be sent as individual values so that anly records associated with those records will be converted by the pdf. I can't see how the urlencode will help this if there are too many
0
 
Dave BaldwinFixer of ProblemsCommented:
The 'urlencode' is make sure the value gets sent correctly.  Space ' ' is not acceptable in a query string and is supposed to be encoded.  It gets automatically decoded at the destination.

As for 'too many', you don't show anything about multiple 'id's in any place above.  Your link to the PDF generator is only one 'id' in your example.  So what are you talking about or trying to do?  Are you creating multiple links with multiple 'id's?  If so, that is even more reason to use only a simple 'id' or key instead of sending a lot of data to the browser window where it will not be actually used but just passed on to the next page.
0
 
Ray PaseurCommented:
This still seems like an odd design pattern for a GET method request -- having so many ID values that you run out of space in the URL string.  To change it to POST, you might use a script like this to start the PDF creation process, but there are many other considerations such as "where do the PDFs go after they are created?"

Moving parts start after line 50

<?php // RAY_curl_post_example.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO USE CURL POST TO START AN ASYNCHRONOUS PROCESS


function curl_post($url, $post_array=array(), $timeout=2, $error_report=FALSE)
{
    // PREPARE THE POST STRING
    $post_string = NULL;
    foreach ($post_array as $key => $val)
    {
        $post_string .= $key . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url         );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE        );
    curl_setopt( $curl, CURLOPT_POST,           TRUE         );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout     );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE         );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"]  = 'Ray';
$args["email"] = 'Ray.Paseur@Gmail.com';

// ACTIVATE THIS TO SEE THE ARRAY OF ARGS
// var_dump($args);

// SET THE URL
$url = "http://LAPRBass.com/RAY_bounce_post.php";

// CALL CURL TO POST THE DATA
$htm = curl_post($url, $args, 3, TRUE);

// SHOW WHAT CAME BACK, IF ANYTHING
if ($htm)
{
    echo "<pre>";
    echo htmlentities($htm);
}
else
{
    echo "NO RESPONSE YET FROM $url -- MAYBE BECAUSE IT IS RUNNING ASYNCHRONOUSLY";
}

Open in new window

HTH, ~Ray
0
 
doctorbillTechAuthor Commented:
Sorry for not being clear
The script:
<?php

$query = "SELECT * FROM orders WHERE agnt = 'CHARLES BENSON'";
       
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
      $qstr .= "&ID[".$row [ID]."]";
     
}
?>

 This is designed to select all records from a database based on an agent name and to then put all the record ID values into a string
The string is : webpage?ID[1]&ID[2]&[ID[3] ......
This string is then used by another php page script  where these values are used to pull up records based on the ID numbers and to return only those records. Those records are used by the fpdf page to generate the pdf document
0
 
Ray PaseurCommented:
Here's where I am not getting it... Why not move the query into the another php page script that pulls up the records based on the ID number?   I do not understand the value-added part of having two separate scripts.
0
 
Dave BaldwinFixer of ProblemsCommented:
Me too.  I would never do that.  You are passing all that data when you could just do the query on the page where the PDF is created and not have to pass all that around thru query strings.
0
 
doctorbillTechAuthor Commented:
The problem was that the code structure was already present but I had not implemented it correctly
The team highlighted my mistake
excellent work guys !!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now