doctorbill
asked on
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?<?ph p 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)?
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?<?ph
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)?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?<?ph p echo $qstr; ?>"><strong>Save To PDF:</strong></a>
Then the database query becomes the first thing in 'tcpdfOutput.php'.
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?<?ph
Then the database query becomes the first thing in 'tcpdfOutput.php'.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
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.
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
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";
}
HTH, ~Ray
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 !!
The team highlighted my mistake
excellent work guys !!
ASKER