Solved

php POST GET

Posted on 2013-01-14
11
296 Views
Last Modified: 2013-01-18
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
Comment
Question by:doctorbill
  • 4
  • 4
  • 3
11 Comments
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 38775529
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
 

Author Comment

by:doctorbill
ID: 38776250
can you give me an example of an identifier I could use please
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38776369
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 38777816
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
 

Author Comment

by:doctorbill
ID: 38784160
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 38784257
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38784922
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
 

Author Comment

by:doctorbill
ID: 38789604
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 38789633
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
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 38789742
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
 

Author Closing Comment

by:doctorbill
ID: 38793790
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now