Page 1 of 1

sending $_POST

Posted: Mon Oct 16, 2006 1:49 pm
by KSquared
Hi All,

Im having trouble with the syntax for the following:

I have a page that captures some form vars. After writting these values to a csv file I then to take the name/value pair and send them using $_POST not GET to a secure page so they may be submitted to an email db.
Im not sure how I send these via $_POST instead of page.php?var1=value1& etc....

Anyone steer me in the right direction?

Thanks

Posted: Mon Oct 16, 2006 1:52 pm
by John Cartwright
So you want to send these values as a post without submitting a form? If thats the case you on option would be cURL. Have a read, give it a try and if you have any more problems let us know.

Posted: Mon Oct 16, 2006 1:58 pm
by KSquared
Thank you, Ill have a look.

Posted: Mon Oct 16, 2006 2:08 pm
by RobertGonzalez
You can also use sessions, cookies or javascript to force a post.

Posted: Tue Oct 17, 2006 1:20 pm
by KSquared
I have done some research on CURL as you have suggested.

Here is my delima:

Some form data is submitted from "register.php" to "registerdb.php". registerdb.php connect to a db checking to see if the username is already taken. If it is, I need to send all form data back to the register.php, so the user does not have to fill out all fields again.

Here is what I have: (first I do some checking... left that out of snippet)

Code: Select all

$postArray = "fullname=".urlencode($fullname)."&title=".urlencode($title);  //this is just part of my form data used for example

$url = "http://www.geneva-scientific.com/includes/register.php";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray); 
$results = curl_exec($ch); 
echo $results; 
curl_close($ch);
This seems to be loading my form twice on registerdb.php page. If I remove the echo $results; line it just displays a blank registerdb.php page.

Im trying to get it to redirect back to register.php, fill in the fields, and tell the user the "username" has been taken. However, it is sending back the post data, as I can echo the data, but loading to the wrong page.
Im confused how the redirect works.... How do I handle this?

Thanks,
Keith

Posted: Tue Oct 17, 2006 1:34 pm
by RobertGonzalez
You know, I think I may have overthought this one. You are getting information from a form right? And that information is being processed into a CSV file right? Why can't you wrap the CSV stuff inside the db page so they are done together?

Posted: Tue Oct 17, 2006 1:59 pm
by KSquared
Everah,

Just to clarify, the post above is a different issue with a different site and different problem.

But, regarding your post, yes, I will being doing both CSV and db stuff together on one page. The db is simply used for a backup in case something odd happens to the csv file.
I then need to take those same name=value and pass them using POST to a secure Email campaign service. (Exact Target)
So my confusion is how to POST them without a user actually pressing submit.

And after finding some info on CURL, I believe I could do the following?

Code: Select all

@extract($_POST);
$postArray = "fullname=".urlencode($fullname)."&title=".urlencode($title); 

$url = "https://www.someurl.com/"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray); 
$results = curl_exec($ch); 

curl_close($ch);

Posted: Tue Oct 17, 2006 2:20 pm
by feyd
Looks about right, provided "fullname" and "title" were in the submission. I would suggest staying away from extract() however, or at least check that the fields you are expecting are there and you filter out any other fields submitted.

edit: grammar.

Posted: Tue Oct 17, 2006 2:30 pm
by KSquared
Thanks feyd,
is the traditional $myVar = $_POST['whatever']; the best way to go, even if I have many submissions?

Posted: Tue Oct 17, 2006 2:52 pm
by KSquared
Going back to my post about returning vars back to my form, I figured out why it my form was displaying twice. A loop was firing twice... duh...
anyway, the only issue Im still having is that when I submit the form:

it loads my register.php page on my registerdb.php page. So in my address bar is http://www.site.com/includes/registerdb.php... is that just how CURL works or can I have the actual regester.php page be the one to display?

So by setting

Code: Select all

$url = "http://www.geneva-scientific.com/includes/register.php";

curl_setopt($ch, CURLOPT_URL,$url);
shouldnt that redirect to register.php? or is that now how CURL works?

Sorry for the lame posts, I just really want to get my head around this.

Thanks,

Posted: Tue Oct 17, 2006 2:59 pm
by John Cartwright
It won't redirect to the page, it will call the page from the current script and capture the output. (ie. curl_exec());

Posted: Tue Oct 17, 2006 4:40 pm
by KSquared
Im getting an odd statement:
When I send my data to email_updates.php, I get this after I hit submit "Object moved to here." "here" being a link to the actual page that needs to load. I get the return url from the form page, see below: $thx, $err, $usub, then the data is sent to email collection page and it returns the url depending on what happens (ie. error, success).

Code: Select all

$SubscribeDate = date("F j, Y");
$fname = $_POST['First Name'];
$lname = $_POST['Last Name'];
$emailAdd = $_POST['Email Address'];
$source = $_POST['Source'];
$thx = $_POST['thx'];   //http://www.site.com/thankyou.htm
$err = $_POST['err'];    //http://www.site.com/error.php
$usub = $_POST['usub'];

************* will write to csv file here **************************


$postArray = "First Name=".urlencode($fname)."&Last Name=".urlencode($lname)."&Email Address=".urlencode($emailAdd)."&Source=".urlencode($source)."&thx=".urlencode($thx)."&err=".urlencode($err)."&usub=".urlencode($usub)."&Subscribe Date".urlencode($SubscribeDate); 

$url = "http://cl.exct.net/subscribe.aspx?lid=55555555"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray); 
$results = curl_exec($ch); 
echo $results;
curl_close($ch);
Arggg... I dont know any other methods to send these through POST except CURL...

Thanks again