Page 1 of 1

PHP Remote Form Help

Posted: Tue Jun 13, 2006 2:09 pm
by staypuftman
Hello forum...I'm a bit new to PHP but I think I have the basics down. Problem is what Im trying to do is a bit beyond the basics I fear.

Just so you know, my whole site is written in HTML but all the page extensions end in PHP because I use PHP to call the header and footer of my site. Anyhow, I am trying to get the user to input an email address into a text box. The text box form looks like this:

Code: Select all

<form action="/blog_features/newsletter_mail.php" method="post" name="newsletter" id="newsletter">
						<input type="hidden" name="m" value="1101275763224">
						<input type="hidden" name="lang" value="en">
						<input type="hidden" name="loginName" value="name">
						<input type="hidden" name="loginPassword" value="password">
						<input type="hidden" name="ic" value="frontpage_signups"> 
						<input type="text" name="ea" size="20" maxlength="50" value=""><br /> 
						<input type="submit" value="&nbsp;Sign Me Up!&nbsp;" id="submit" class="active">
                                         </form>
Then the system should hit newsletter_mail.php and do 4 things:

1) Put the hidden input fields loginname, loginpassword, ic and user provided ea field into a database located on a DIFFERENT server that MUST be accessed via a script
2) Email the admin, alerting him that he has a new subscriber
3) Email the new user, letting them know they have been successfully added to the mailing list
4) Dump the user onto a thank page via a redirect

This is the code I've come up with so far to do this:

Code: Select all

<?php
	// variables passed from header.inc.php for constant contact script
	$email = $_REQUEST['ea'];
	$login_name = $_REQUEST['loginName'];
	$login_password = $_REQUEST['loginPassword'];
	$interest_group = $_REQUEST['ic'];
	
	// variables to send howard a new user signup confirmation email
	$howard_address = "john.serrao@humanproductivitylab.com";
	$howard_body = "Howard, Some guy with ".$email" for an email address just signed up.  He was automatically added to the constant contact system in the interest group 'frontpage_signups.'  If you wish to change his interest group, login to the constant contact system, choose the 'Subscriber & Lists' tab > Interest Groups.  THIS IS AN AUTOMATICALLY GENERATED EMAIL - DO NOT RESPOND.";
	$howard_subject = "HPL SYSTEM UPDATE: Newsletter Signup Notification";
	
	// variables to send new subscriber a confirmation email 
	$newsubscriber_address = $email
	$newsubscriber_body = "You have been successfully added to the HPL's newsletter list 'The Art of Productivity.'  Thank you for your support, HSL"
	$newsubscriber_subject = "Thank you for signing up for the HPL newsletter!"
	
	//plug variables from header.inc.php into the form
	########dont know how to do this part############
	
	// send emails to howard and new subscriber
	mail ($howard_address, $howard_subject, $howard_body);
	mail ($newsubscriber_address, $newsubscriber_subject, $newsubscriber_body);
	
	// redirects user to 'thank you' page
	header( "Location: http://www.humanproductivitylab.com/blog_features/thank_you.php" );
?>
As you can see there is a giant hole in my code where the variables passed from the header file should be put into the remote script. How do I get those variables to be passed to a script?

If it helps you, the script is located at:
http://ccprod.roving.com/roving/wdk/API ... isitor.jsp
(it is not my script, but i have tested it and it works very well)

If you can help me, let me know
john

Posted: Tue Jun 13, 2006 2:23 pm
by Christopher
Can you clarify which of the 4 points you list above go with which section of code. Better would be to remove any code that you do not have questions about leaving only the code you do have a question about.

Posted: Tue Jun 13, 2006 2:29 pm
by staypuftman
Its the part of the code above that is missing - its read as:

Code: Select all

//plug variables from header.inc.php into the form
   ########dont know how to do this part############
I included the rest of the code because it appears to be vital to solving the problem.

I have the 4 variables (loginname, loginpassword, ic, and ea) that were successfully passed from the form in my header file over to the php script file. Once those variables are in the php script file (like they are now), how do I get them to be routed over to the remote script that actually enters the variables into the database? Is it some kind of send function I am not aware of?

thanks
john

Posted: Tue Jun 13, 2006 3:14 pm
by Christopher
staypuftman wrote:Once those variables are in the php script file (like they are now), how do I get them to be routed over to the remote script that actually enters the variables into the database? Is it some kind of send function I am not aware of?
I'm not sure what "routed over to" means. I am guessing that you want to save the values in a database that is on a different server than this form is on. You can pass them as parameters in the redirect URL (may not work with all browsers) or use the cURL library to access the other server from within your script.

I'll break it way down

Posted: Tue Jun 13, 2006 3:28 pm
by staypuftman
I have 4 variables from the original HTML form
loginname
loginpassword
ic
ea

The first 3 are hidden variables that never change but must be passed. The fourth, 'ea' is the user provided email address. All four of these get passed from the form to a seperate PHP scripting page I made (newsletter_mail.php).

On the PHP scripting page I made, those four variables get redefined into PHP terms (see code above). All four of them MUST be forwarded onto yet another scriping page I did NOT make:
http://ccprod.roving.com/roving/wdk/API ... isitor.jsp

This remote scripting page is the one that does the hard work, putting the variables into their correct positions and storing the email into the newsletter list database. The reason for doing seemingly unecessary double variable pass is because this remote scripting page is not mine, so I cant add things like auto generated emails and redirect thank you pages. Its a stock script this newsletter company came up with. These other features like email and forwarding are essential for the functionality I need.

The real question is how do I send the four variables from my staging scripting page onto the real scripting page that puts them into the database?

thanks - I hope this helps
john

Posted: Tue Jun 13, 2006 3:38 pm
by Christopher
Use the cURL library. The manual has a number of examples.

Another new world, cURL

Posted: Tue Jun 13, 2006 4:41 pm
by staypuftman
Alright, I set things per the instructions but no dice - here is the code

Code: Select all

<?PHP
	// variables passed from header.inc.php for constant contact script
	$email = $_REQUEST['ea'];
	$login_name = $_REQUEST['loginName'];
	$login_password = $_REQUEST['loginPassword'];
	$interest_group = $_REQUEST['ic'];
	
	// curl setup
	$ch = curl_init();
	
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_URL, "http://ccprod.roving.com/roving/wdk/API_AddSiteVisitor.jsp");
	curl_setopt($ch, CURLOPT_POST, $login_name);
	curl_setopt($ch, CURLOPT_POST, $login_password);
	curl_setopt($ch, CURLOPT_POST, $interest_group);
	curl_setopt($ch, CURLOPT_POST, $email);

	// grab URL and pass it to the browser
	curl_exec($ch);
	
	// close CURL resource, and free up system resources
	curl_close($ch);
?>
My first thought it, how does the script know which database slots to put the cURL requests into. As it stand, cURL is just dumping 4 variables on the script's lap - how do I define where the script dumps the variables?

any other thoughts?
serrao

Posted: Tue Jun 13, 2006 5:19 pm
by derchris
You say that three of the hidden fields never change.
But why do you Include them in the form?
Why not put them in the script, so that nobody sees them if it a user / password you need.

Posted: Tue Jun 13, 2006 5:58 pm
by Christopher
I think you need to look more closely at the documentation for curl_setopt(), CURLOPT_POST and CURLOPT_POSTFIELDS.

more curling

Posted: Tue Jun 13, 2006 6:22 pm
by staypuftman
derchris-

I guess thats a good point - I guess I only need to define them in the scripting page I made. That still shouldnt have any effect on the functionality dealing with the curls, should it? I think I will just leave this as is for now until i get through the other more difficult problem.

arborint-

This is all it says in the manual about what you are talking about:

curloptpost seems to be the right choice after further review because it accepts strings, are the variables read as strings??

Here is the new code I came up with:

Code: Select all

<?PHP
	// variables passed from header.inc.php for constant contact script
	$email = $_REQUEST['ea'];
	$login_name = $_REQUEST['loginName'];
	$login_password = $_REQUEST['loginPassword'];
	$interest_group = $_REQUEST['ic'];
	
	// curl setup
	$ch = curl_init('http://ccprod.roving.com/roving/wdk/API_AddSiteVisitor.jsp');
	
	// set URL and other appropriate options
	curl_setopt($ch, CURLOPT_POSTFIELDS, $login_name);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $login_password);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $interest_group);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $email);

	// grab URL and pass it to the browser
	curl_exec($ch);
	
	// close CURL resource, and free up system resources
	curl_close($ch);
?>
Still not working - any ideas? I've gotta get outta here for 3 hours of so, but I'll be checking back on this tonight. Any further help would be much appreciated.

thanks
john

Posted: Tue Jun 13, 2006 6:34 pm
by Christopher
Here is a link to an example in the online manual:

http://www.php.net/manual/en/ref.curl.php#62274

curl nightmare

Posted: Wed Jun 14, 2006 1:21 pm
by staypuftman
So I read through the curl example on how do this properly - except it does not once explain transferring passwords or email addresses through urls. It seems to be explaining file transfers...I guess thats the same thing in PHP world?

Anyhow, I recoded the example on the page to 'match' my needs - again it doesnt work.

Code: Select all

// curl sequence to dump new email addresses into CCDB
   $post_data = array(); //setup a long array to grab all variables
  
   $post_data['loginName'] = $_REQUEST['loginName']; //stick each variable from HTML form into the array
   $post_data['loginPassword'] = $_REQUEST['loginPassword'];
   $post_data['ea'] = $_REQUEST['ea'];
   $post_data['ic'] = $_REQUEST['ic'];

   $ch = curl_init(); //intiate curl sequence
   curl_setopt($ch, CURLOPT_URL, "http://ccprod.roving.com/roving/wdk/API_AddSiteVisitor.jsp" ); //URL  of transfer location
   curl_setopt($ch, CURLOPT_POST, 1 ); //sets up array for transfer I think
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //grabs the array I hope...
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //beats the hell out of me
   $postResult = curl_exec($ch); //setup a postresult variable that executes the session

   if (curl_errno($ch)) {  //prints error if one occurs
       print curl_error($ch);
   }
   curl_close($ch); //closes session
   print "$postResult"; //prints the disaster area
It seems like there is some giant concept Im missing here. As far as I understand, this is the logic of the latest example:
1-establish variable as array
2-dump all string variables into array for single transfer
3-intiate curl, establish URL
4-curlopt_post readies system for htmlpost-like transfer
5-curlopt_postfields actually transfers the array
6-no idea what returntransfer does

If someone could explain the general logic or what the hell curlopt_returntransfer is doing better than the manual, I would be grateful.
very lost
john