Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 12-22-2011, 05:46 AM Prefill forms
Super Talker

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
I am looking for a way to fill out a form, let me try to explain.

I have a website that I am creating, this is a careers website, basically people come to it that are looking for employment.
I would like that a user enters all their details into a form and once the form is submitted it not only adds all the details to my websites database but at the same time submits to 3 other websites that also deal with careers. I have accounts with these 3 websites and can post new CVs to their respective websites, but it is alot of leg work, I have to go to the first and enter in 30 fields, then the next and then the next, I have asked all 3 of these sites if they could provide me with an API and none of them are even remotely interested in handing it out to me.
Can anyone think of a way that I can do this.
Even if there is a way that I can go to their site and then hit a button that completes the form for me with my correct details.
__________________
This was my latest holiday in
Please login or register to view this content. Registration is FREE
South Africa.
This was my favorite adventure holiday in
Please login or register to view this content. Registration is FREE
scorpioserve is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-22-2011, 07:24 AM Re: Prefill forms
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
With some extremely complicated fsockopen() parameters, it can be done. However, the code would be long, and I have no clue on how to write it!

I once wrote a function that could submit POST data to a website, then return a result through fsockopen(). I'll see if I can dig up that code when I get home.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-22-2011, 07:28 AM Re: Prefill forms
Super Talker

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
That would be awesome thanks.
__________________
This was my latest holiday in
Please login or register to view this content. Registration is FREE
South Africa.
This was my favorite adventure holiday in
Please login or register to view this content. Registration is FREE
scorpioserve is offline
Reply With Quote
View Public Profile
 
Old 12-22-2011, 07:30 AM Re: Prefill forms
Super Talker

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
Sorry I have to tell you that I love your avatar, I must have laughed for at least 10 minutes, there's no place like localhost would have lost it's appeal.
__________________
This was my latest holiday in
Please login or register to view this content. Registration is FREE
South Africa.
This was my favorite adventure holiday in
Please login or register to view this content. Registration is FREE
scorpioserve is offline
Reply With Quote
View Public Profile
 
Old 12-22-2011, 07:34 AM Re: Prefill forms
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Thank you!

I found the code, here it is!

PHP Code:
function remote_method($u,$m,$v,$vc) {
    
//This function is an adapted version of php.net's context options page
    
$p=http_build_query(array($v=>$vc));
    
$o=array('http'=>array('method'=>$m,'header'=>'Content-type: application/x-www-form-urlencoded','content'=>$p));
    
$c=stream_context_create($o);
    return 
file_get_contents($u,false,$c);

Usage:
PHP Code:
echo remote_method("http://remote.server.com","POST","form-post-input","form-post-input-content"); 
I modified the script from php.net's context options page, originally for a church website. Just to see how it works, try this code:
PHP Code:
echo remote_method("http://bible.oremus.org","POST","passage","Mark 1.1-11"); 
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-22-2011, 07:40 AM Re: Prefill forms
Super Talker

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
Yay thanks I will give it a try and see how it goes, thanks. It certainly looks very interesting.
__________________
This was my latest holiday in
Please login or register to view this content. Registration is FREE
South Africa.
This was my favorite adventure holiday in
Please login or register to view this content. Registration is FREE
scorpioserve is offline
Reply With Quote
View Public Profile
 
Old 12-22-2011, 03:15 PM Re: Prefill forms
Super Talker

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
Probably a very silly question, but what if I wanted to complete more than a singe textfield, would I just add another one like this
PHP Code:
echo remote_method("http://bible.oremus.org","POST","passage","Mark 1.1-11","textfield2","textfield2 contents"); 
I have tried it on my test site, but it only seems to deal with the first one in the list and ignore any others. Any ideas.
__________________
This was my latest holiday in
Please login or register to view this content. Registration is FREE
South Africa.
This was my favorite adventure holiday in
Please login or register to view this content. Registration is FREE
scorpioserve is offline
Reply With Quote
View Public Profile
 
Old 12-22-2011, 03:52 PM Re: Prefill forms
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
First of all, sorry for not posting actual fsockopen code! Looking at it again made me realize that it's actually using contexts and file_get_contents. Oops!

The reason your code isn't working is because the function isn't built like that. The function asks for 4 parameters, so 6 won't work because it doesn't know what they even are.

I'll see if I can rewrite the code to allow for arrays, or something that will allow more than one request to be made in one execution.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-22-2011, 04:22 PM Re: Prefill forms
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Alright, here we go!

PHP Code:
function remote_method($url,$method,$var,$varc) {
    if(
is_array($var)&&is_array($varc)){
        if(
count($var)==count($varc)){
            
$keys=array();
            
$vals=array();
            for(
$i=0;$i<=count($var)-1;$i++){
                
array_push($keys,$var[$i]);
                
array_push($vals,$varc[$i]);
            }
            
$params=array_combine($keys,$vals);
        }
        else{die(
'count for keys & values must be the same');}
    }
    else{
$params=array($var=>$varc);}
    
$postdata=http_build_query($params);
    
$opts=array('http'=>array('method'=>$method,'header'=>'Content-type: application/x-www-form-urlencoded','content'=>$postdata));
    
$context=stream_context_create($opts);
    return 
file_get_contents($url,false,$context);

It hasn't really been tested, other than for fatal errors. In theory, it should work though!

The function works the same as before, except now you can supply an array value for the third and fourth value. The third value are the 'form field names', like 'username' and 'password'. The fourth value are the corresponding values for the form fields, like 'Physicsguy' and 'myPass'.

I personally think the code is pretty beastly, but it hasn't been tested on the kind of stuff you need, so please let me know if you get any errors or it doesn't do what you need.

As well, if you do use arrays as the 3rd and 4th values, the arrays need to be the same length. If one array has more values than the other, it will return a fatal error. Example:

PHP Code:
remote_method("http://google.ca","POST",array('param1','param2'),array('value1','value2')); 
will work, but
PHP Code:
remote_method("http://google.ca","POST",array('param1','param2','param3'),array('value1','value2')); 
will not.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-22-2011, 04:48 PM Re: Prefill forms
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Found a bug. Either that or the login form I'm using is just crappy.

Try this instead, either way, maybe it'll work for you if the other code doesn't!

PHP Code:
function remote_method($url,$method,$var,$varc) {
    if(
is_array($var)&&is_array($varc)){
        if(
count($var)==count($varc)){
            
$keys=array();
            
$vals=array();
            for(
$i=0;$i<=count($var)-1;$i++){
                
array_push($keys,$var[$i]);
                
array_push($vals,$varc[$i]);
            }
            
$params=array_combine($keys,$vals);
        }
        else{die(
'count for keys & values must be the same');}
    }
    else{
$params=array($var=>$varc);}
    
$postdata=http_build_query($params);
    
$opts=array(
        
'http'=>array(
            
'method'=>$method,
            
'header'=> "Content-type: application/x-www-form-urlencoded\r\n".'Content-Length: '.strlen($postdata) . "\r\n",
            
'content'=>$postdata)
        );
    
$context=stream_context_create($opts);
    
$handle=fopen($url,'r',false,$context);
    while((
$buffer=fgets($handle,4096))!==false){$o.=$buffer;}
    return 
$o;
    
fclose($handle);

Use it in the same way. This one opens the URL differently, which I basically copied off of somebody's comment on a php.net manual page.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-23-2011, 02:54 AM Re: Prefill forms
Super Talker

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
Who da man, you da man, thanks so much your second and your third codes are both working perfectly with the form that I am testing, thank you so very much, now all I got to figure out is how to get the response. Something like this, so with your third code I submit the form data, the form either comes back with a success or failure landing page, is there a way that I could get either page so I could show the user if the request succeeded or failed.

here is my form I am testing this with
PHP Code:
echo remote_method("http://www.homeimprovementguide.co.za/scripts/comments_form.php","POST",array('comments_firstname','comments_email','comments_lastname','comments_company_name','comments_telephone','comments_description'),array('Paul','admin@psweb.co.za','Strydom','PsWeb','031 762 1583','This is what the description area should say'));
?> 
__________________
This was my latest holiday in
Please login or register to view this content. Registration is FREE
South Africa.
This was my favorite adventure holiday in
Please login or register to view this content. Registration is FREE
scorpioserve is offline
Reply With Quote
View Public Profile
 
Old 12-24-2011, 03:03 AM Re: Prefill forms
Super Talker

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
Now I have hit quite a snag, there are 2 places I need to use this script on, both of these places however use a login form first, then once logged in I can go and complete the form I want to use, is there a way that I can first submit the login parameters and then submit the form, is this possible.

Second issue is this, one of the places uses asp and not php, and to be honest I have never coded a single line of asp in my life, so I have no clue as to what I am doing with regard to that one.

Any ideas.
__________________
This was my latest holiday in
Please login or register to view this content. Registration is FREE
South Africa.
This was my favorite adventure holiday in
Please login or register to view this content. Registration is FREE
scorpioserve is offline
Reply With Quote
View Public Profile
 
Old 12-24-2011, 07:12 AM Re: Prefill forms
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Quote:
Originally Posted by scorpioserve View Post
Second issue is this, one of the places uses asp and not php, and to be honest I have never coded a single line of asp in my life, so I have no clue as to what I am doing with regard to that one.
No matter what language they're using server side, they still handle data according to the same HTTP protocol, just as you're doing when sending the requests. You should be able to use the same code either way.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-24-2011, 03:38 PM Re: Prefill forms
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Yep, lizciz is right. As long as your using HTTP to do the requests (which my script does) it's the site that's doing all the work.

If you want to log in, then do something, I suppose you'd had to use the script twice, in some weird kind of way. One for logging in, the other for 'doing something'. I don't know if PHP keeps session cookies active over multiple requests, or even if PHP accepts it's own cookies or not. Either that, or do everything in one call, but I'm not sure if that will work or not.

I don't have much of a clue, either. This might help, but you need cURL, which is an extension for PHP that, from my understanding, is very powerful.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Prefill forms
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.49757 seconds with 12 queries