Forum Moderators: coopster

Message Too Old, No Replies

Integrate client form with third party form

or "how to make their form action work with mine"

         

doodlebee

12:00 am on Oct 14, 2005 (gmt 0)

10+ Year Member



I know, I'm just full of questions today. It really sucks when you have everything done, and then your client pulls a bunch of stuff on you that you weren't aware of, and you have to rewrite everything.

Anyway, here's the deal. The client wants a certain form to have its input send to a third party. The form is *only* an email address - that's it. Looks like this:


<?

if ($action == "sendmail"){
if ($error == "")
{ echo "<span class=\"error2\"><b>Thank You!</b> &nbsp; You're now signed up for the Company Newsletter, the foremost authority on developing quality requirements specifications. Watch for it in your inbox!</span>"; }}

?>

<?php

if ($error == "1") {
echo "<span class=\"error2\">";
}
if ($emailerror == "1")
{ echo "The e-mail you entered was invalid. Please check it and try again."; }

if ($error == "1")
{ echo "</span>"; }

?>

Sign up for our FREE newsletter on <b>developing quality requirements specifications.</b><br />

<form method="post"
name="newsletter"
action="<? echo $PHP_SELF?>">

<input type="hidden" name="recname" value="Company Name" />
<input type="hidden" name="subject" value="Newsletter Email Submission" />

<b <? if ($emailerror!= "")?> >Email:</b></span>
<input class="input" type="text" name="email" size="20" value="<? echo $email?>" />

<input type="hidden" name="action" value="sendmail" />
<input class="button" type="submit" value="Send It" />
</form>

See? Simple. One field. If the end user does not input an email address, or inputs an invalid string as an email address, it returns the error.

The PHP code is as such:


<?

isset($_POST['action'])? $action = $_POST['action'] : $action = '';
isset($_POST['email'])? $email = $_POST['email'] : $email = '';

if($action!="sendmail"){
include ("home.shtml");
exit;}

if ($action == "sendmail") {

if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+'
.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{¦}~]+\.'
.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+$', $email) ¦¦ ereg("'", $email)) {
$emailerror = "1";
$send = "no";}

if ($send == "no") {
$error = "1";
include ("home.shtml");
exit;}

$to = "email@company.com";

$message = nl2br("Please add my e-mail to your newsletter mailing list.

e-Mail address: $email");

$mailheader = "From: <$email>\nContent-Type: text/html";

mail($to, $subject, $message, $mailheader);

}

{
include ("home.shtml");
exit;
}

?>

More simplicity. Gotta love it.

Now I find out I need to make it work with this third party service. Unfortunately, the only code they provide is for HTML based foms *only*. (Yes, I actually called them on the phone to ask for assistance, and they basically told me to figure it out for myself, because they don't provide support for anything but generating HTML code. Nice.)

So, here's their code that somehow needs to be integrated into mine (I'm changing some stuff, because I don't know what it does - but I mean like the numbers they make me input for things - I don't know if they're client-specific or not):


<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8">
------------------->
<form action="3rdParyURL/servlet.WebToLead?encoding=UTF-8"
method="POST">

<!------------------
This HTML is defaulted to DEBUG mode so that your web master can test the online Web-to-Lead page from the desktop. Once your web master has finished testing please remember to remove the entire "debug" and debugEmail" lines of code prior to adding this HTML to
your website.
------------------->
<input type="hidden" name="debug" value=1>
<input type="hidden" name="debugEmail" value="me@myemail.com">

<input type=hidden name="oid" value="123456789065432">
<input type=hidden name="retURL" value="http://www.companysite.com">

Email: <input name="email" id="email" type="text" size=20 maxlength=80><br>

newsletter request: <input type="checkbox" name="56A987123J5489P"
value="1" id="752D58963G1247W"><br>

<input type="submit" name="submit">
</form>


Now, you will notice that they have a checkbox for adding someone to the newsletter - but the client just wants the sender sending the e-mail the only thing they need to do to get this going.

I have *no* clue how to integrate this into my PHP. I can only have the e-mail area, and I don't know what to do about the action - if I replace my <? echo $PHP_SELF?> with their action - it negates my page. not completely, though - but for the most part.

Would anyone have any suggestions for me? I'd really appreciate it!

[edited by: ergophobe at 4:09 pm (utc) on Oct. 14, 2005]
[edit reason] anonymized, fixed sidescroll [/edit]

doodlebee

4:21 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



Okay, well, I've gotten a little further with this on my own. I'm about 2/3 of the way to where it's working as it should, but I'm having an issue I still can't seem to bridge.

I kept the PHP file as is - didn't touch it. But what I did change was this:

<form method="post" name="newsletter" action="THEIR URL HERE">

<span class="menulist">
<b <? if ($emailerror!= "")?> >Email:</b></span>
<input class="input" type="text" name="email" id="email" size="20" value="<? echo $email?>" />

<input type="hidden" name="NEWSLETTER STUFF HERE" value="1" id="MORE OF THE SAME">

<input type="hidden" name="action" value="submit" />
<input class="button" type="submit" value="Send It" />
</form>

Now, to explain, I *can't* have the checkbox, so I put it in a hidden tag. That worked great.

I've also tried to use $_POST['email'] set as the value (I tried *that* as a hidden field first, but it had an interesting error - so it was promtly changed back!), but I had pretty much the same problem with the way it's set in the code up there right now as if I had used the POST value - which is the next thing coming...

Like I said, I'm almost there now. But the current issue is that my error message is not being returned when invalid (or no) input is put in the email field. Doesn't matter what you do, if you click "submit", the form now accepts anything you put in there (or nothing at all) and treats it like an actual submission.

So all I have left to do is figure out *how* to get my error message to show up and not submit anything when invalid input is entered into the field. I'm wondering if I'm going to need to stick the PHP code directly into the HTML file (and rename it php, of course) and toss out the secondary file, since it can't call on itself anymore. Of course, if an error is submitted, it's still going to need to call on itself - so I don't know.

Any suggestions on how to make it *not* submit and toss out an error message for invalid input would be awesome. Other than that, it works great :)

ergophobe

5:08 pm on Oct 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Without looking at you code in detail, it sounds like you need to submit post data to two different servers - your client's and the third-party server.

You can't submit the same post data to two locations, so what you need to do is first validate and, if it does validate, do what you want with the data that's relevant to you and then rePOST the data to the third-party site.

This thread may help you

[webmasterworld.com...]

And this article from Zend covers it too

[zend.com...]

And finally, you can look into the cURL functions.

doodlebee

7:46 pm on Oct 14, 2005 (gmt 0)

10+ Year Member



Well, I figured it out! It may not be pretty, but here's what I did. I stuck it *all* in one file - combined the HTML and the PHP stuff and saved it as a PHP file. Inside, I did this:

<?
isset($_POST['action'])? $action = $_POST['action'] : $action = '';
isset($_POST['email'])? $email = $_POST['email'] : $email = '';
isset($_POST['send'])? $send = $_POST['send'] : $send = '';
isset($_POST['url'])? $url = $_POST['url'] : $url = '';

if ($action == "submit") {

if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+'
.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{¦}~]+\.'
.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+$', $email) ¦¦ ereg("'", $email)) {
$emailerror = "1";
$send = "no";}

if ($send == "no") {
echo "<span class=\"error2\"><b>The e-mail you entered was invalid. Please check it and try again.</span>";
$url = "THIRD PARTY URL CODE HERE";
}

if ($send!="no") {
echo "<span class=\"error2\"><b>Thank You!</b> &nbsp; You're now signed up for the Newsletter. Watch for it in your inbox!</span>";
$url = "";
}}
?>

Sign up for our FREE newsletter on <b>developing quality requirements specifications.</b><br />

<form method="post"
name="newsletter"
action="<? echo $url?>">

<b>Email:</b>
<input class="input" type="text" name="email" id="email" size="20" value="<? echo $email?>" />
<input type="hidden" name="MORE CODE value="1" id="MORE CODE" />

<input type="hidden" name="action" value="submit" />
<input class="button" type="submit" value="Send It" />

</form>

It's probably not the prettiest fix out there, but it works like a charm. Basically, all I did was set it so that the third party URL for submission was set as a variable - if the email was invalid, then the $url would return as empty, and there would be no form submission. If it was valid, it'd stick the right URL in there and off it'd go!

Just thought I'd post it for anyone, in case they needed it in the future. Oh, and by the way, this isn't something that you could use if *you* also needed some of the users input - none of this information gets sent to my client at all - it just goes straight to the third party database.

Hope it helps someone!

[edited by: ergophobe at 12:40 am (utc) on Oct. 16, 2005]
[edit reason] fixed sidescroll [/edit]

doodlebee

7:43 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



Ah, crap. Just discovered this morning that I *didn't* figure it out. The code I have up there works fine, if every person who visits the form put invalid input in fisrt. After the error is returned *then* you can submit valid input.

That's not good.

Anyway, I'm awaiting verification on another attempt at fixing it. I'm 80% sure it worked - but not positive. If I get verification that it *did* work, then I'll post the correct solution here.

ergophobe

10:07 pm on Oct 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's sort of odd that you set the form action based on POST data. Why isn't it just using the same action all the time?

doodlebee

10:18 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



Because if the email that's entered in the field in invalid, or an empty string, then I need for the page to come back on itself and display an error.

The problem is, if I put the 3rd party's URL in the "action" area, it doesn't matter *what* is inputted into the email field, it overrides my script work and submits the data anyway (even if it's empty). I can't have that. I *have* to validate the email to be sure it's in proper email format and/or not an empty field. *Then* I want it to post the form data to the 3rd party URL.

I hope that makes sense!

ergophobe

10:27 pm on Oct 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Actually, it still sounds backwards to me.

Let's say I bookmark the page and I arrive there directly. There is no post data, so there is no action, so the page submits to itself and displays the form again.

Since I've now posted some data, the $url variable is now set and the form now has an action, but you don't want to be using post data to set the action. Doing that you have the order of operations backwards. Post data comes from the previous page. By the time you've validated the input, it's too late to change the action. HTTP simply doesn't work that way.

doodlebee

10:37 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



I know - I figured that out this morning ;)

Now I'm trying to figure out how I can have the action set as the URL in question, but still be able to show my errors if invalid input is made.

It just seems to me that there should be some way to say if ($action == "submit") {
if (error stuff here {
error == 1;
send==no;
blah blah}

//if no errors in input then...

send the info to the stupid URL that they want me to use
}

The problem is, though, that to show my errors, I have to have my action set as $PHP_SELF, but they want me to have my action set as the URL. And if I do that, then when the user clicks "submit" it goes and does what the form action says to do - sends everything (or nothing) to the URL - which I don't want it to do yet. i want it to check the input, and if it's okay, *then* pass it to the URL. But I can't seem to figure out how to do that.

(By the way, I tried your prior links, but I couldn't get them to work. cURL seemed especially promising, until I discovered that they don't have it installed.)

I'm banging my head on this, and they want it done by tomorrow morning. I'm at an utter loss. This just seems like it should be so *simple* - but it's always so *hard*!

(sorry - frustration over! for the moment anyway!)

doodlebee

10:38 pm on Oct 15, 2005 (gmt 0)

10+ Year Member



oh yeah, and the other kicker is that there's a bunch of hidden fields whose input *also* has to be passed, as well. The user's input is the only variable - the hidden fields will always stay the same, but also need to be passed.

ergophobe

12:35 am on Oct 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think you need to conceive of this as a three-step process

1. ask for user input - this is the script that creates your form submission

2. receive and validate user input - this is the "action" of your form

3. pass to third party - this is done by reposting the data and passing it on.

Have you read the threads that I mentioned in my first post and looked up the curl functions [us3.php.net] in the PHP manual?

doodlebee

12:52 am on Oct 16, 2005 (gmt 0)

10+ Year Member



I did read the threads - as I posted before - and I couldn't get those to work. I also gave cURL a try - but they don't have it installed, so it's a no-go. (That looked the most promising!)

So far, I've been working on small test scripts to see if I can get the basics to work. I've been able to 1) have the end user input an email, and if it passes the requirements, it'll 2) pass to a secondary page (one of mine - just to see if I could do it).

I thought maybe once I did that, I could have the secondary page have the form with the passed variables, and *then* submit it with the URL action - but so far I'm not having any luck.

ergophobe

3:28 pm on Oct 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



HTTP just doesn't work that way. You could validate with Javascript, then submit straight to the third party site, but as long as you're going to validate on one site and submit on another, you need to send another HTTP Request either via PHP, curl or whatever.

doodlebee

4:03 pm on Oct 16, 2005 (gmt 0)

10+ Year Member



Well, I finally *did* figure it out, around 1:00 am this morning. It's not pretty, but it works.

Basically, I have 3 files. I have file1.php, which contains all of my error messages and the validation of the email. file 2.html is the layout of the page, which is included in file1.php.

Now, if the end user enters in an email, and it passes muster, then file1.php has a header location which takes the email variable that the end user input, and passes it to file3.php. File3.php sits there with a thank-you message, and I uset a setTimeout javascript function to make the form wait 5 seonds before it auto-submits the email in the form.

There's a chance (at this moment) that the secondary form could quickly - and I mean quickly - have the email changed to use it for spam - but that would still prove to be difficult because the action isn't a mail function. It just sends the data to this third party URL, and if my client chooses not to see it, he just deletes it - it won't go anywhere else. But I think I'd still like to put in a check, just to be sure there's no attempts to try and change the e-mail field in that 5-second window.

I know it ain't pretty, and there's probably better ways to handle this - but as I told them a long time ago, I'm not a PHP back-end developer, and I can't believe they really thought this was up my alley. They seem to think I'm some kind of PHP guru, simply because I once made a mail form that worked. God forbid someone figures that out on their own, you know? Anyway, they needed this site *today* which is why i was under pressure to find a solution - *any* solution - that would work. I did. Not perfect, but there it is.

I do, however, thank you for all your help - and in the future, I'll definitely be trying out that cURL system - that seem like an awesome little function there.

jatar_k

3:21 pm on Oct 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hey doodlebee,

I am glad you figured it out but I wanted to offer something a little different.

Sometimes we need to do as many things as possible for the user and sometimes we need to give them another step or two to make everyone's life easier.

For your particular scenario I see adding another step for the user as being a viable solution. I see this as being the same as what ergophobe was explaining but it would not need curl.

I see three files

1. myform.html
2. validationscript.php
3. confirmation.html

now, myform.html is just that, your form asking for user input and with the form action always set to validationscript.php.

validationscript.php allows you to validate all user input and if there is an error you can redisplay myform.html with the fields that need editing. If there are no errors then you can display confirmation.html. The other option you have with this validation script is you can save/write the data somewhere to your server if there is need.

confirmation.html just displays the data in a non editable format and asks the user if the information is correct. This page will have a submit button. The rest of the information you wish to submit is written to hidden fields within this form. The action of the form is set to your third party url.

that make any sense? adding the second step for the user in a way they are used to simplifies your need for rewriting actions.

doodlebee

4:24 pm on Oct 17, 2005 (gmt 0)

10+ Year Member



i jatar :)

Actually, that's precisely what I did! I guess I'm just not good at explaining things, eh?

But thanks for the tip!

jatar_k

4:47 pm on Oct 17, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well then my apologies for misunderstanding, I did read your post three times

>> It's not pretty, but it works.

then the above statement was in error, you did exactly what needed to be done and it is as pretty as it can be. ;)

ergophobe

4:51 pm on Oct 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ahh. Okay, I didn't realize you were willing to make it a two-step process for the user. As Jatar_K said, my comments only apply if you want to make one page submit to two different addresses.

I'm glad it's working for you.