How to run a PHP function before executing an external Perl Script?
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

On form submit, I need to run a PHP function, just before executing a Perl Script.

See this form code:

<form id="myform" method="post" action="https://www.aweber.com/scripts/addlead.pl">

Before this Aweber.com script runs, I need to run some PHP code to manipulate the values in the form.

So my question is:

On form submit, how do I run some PHP code BEFORE running this addlead.pl script?

There is no simple and direct solution. But there can be other ways:
  1. Is there a possibility to change post action? If so, route to PHP script, which will manipulate form data and then run Perl script, passing data.
  2. If you can't change form action, and you using nginx, you can rewrite request from /scripts/addlead.pl to (for example /scripts/addlead_pre.php
  3. If neither of this suffice, you can modify your script, to run PHP script itself, pass PHP params, and then receive back modified ones.
yunasc over 7 years ago
Hi. Yes you can change action But you cant change the addlead.pl script. Because that's owned by aweber. assumedly theres a way to do it. if i change action=my-process-routine.php then i can somehow put a function in here and then call addlead.pl
tonloc over 7 years ago
So, since you can't modify Perl script, and neither you can modify web config of aweber. I suggest you using some JavaScript on page, to change FORM action (if you can't directly change action). Are you sure, that you need PHP for this? Maybe you just need some jQuery code (hooked to onSubmit event), on client side? (or this is something super-secret stuff? ;)
yunasc over 7 years ago
Tags
PHP

Crowdsource coding tasks.

2 Solutions


//Server-side Solution:

1) you need to change form action to php page:

<form id="myform" method="post" action="my_php_page.php">

2) The php must redirect/POST data to perl page:

<?
//your code


//Thanks to: http://stackoverflow.com/questions/5576619/php-redirect-with-post-data
/**
 * Redirect with POST data.
 *
 * @param string $url URL.
 * @param array $post_data POST data. Example: array('foo' => 'var', 'id' => 123)
 * @param array $headers Optional. Extra headers to send.
 */
 public function redirect_post($url, array $data, array $headers = null) {
$params = array(
    'http' => array(
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);
if (!is_null($headers)) {
    $params['http']['header'] = '';
    foreach ($headers as $k => $v) {
        $params['http']['header'] .= "$k: $v\n";
    }
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if ($fp) {
    echo @stream_get_contents($fp);
    die();
} else {
    // Error
    throw new Exception("Error loading '$url', $php_errormsg");
}
}



//Redirect to PERL page
redirect_post("https://www.aweber.com/scripts/addlead.pl", $_POST);
cool thanks man. The only thing is, after the addlead.pl script runs over at Aweber, then it redirects back to the myphppage.php URL. Can you think of why its coming back there?
tonloc over 7 years ago
because in php is impossible to make a post request changing the url of the page, if you need to change the url you need a client-side solution.
Stefano Balzarotti over 7 years ago

//Client-side solution:

1)You need to prevent HTML post in javascript and create an ajax request to the php page:

<form id="myform" method="post" action="https://www.aweber.com/scripts/addlead.pl" onsubmit="customPost("my_php_page.php", this, event); return false;">  

2)Javascript code to do custom call to php page:

function serialize(form, evt){
var evt    = evt || window.event;
evt.target = evt.target || evt.srcElement || null;
var field, query='';
if(typeof form == 'object' && form.nodeName == "FORM"){
    for(i=form.elements.length-1; i>=0; i--){
        field = form.elements[i];
        if(field.name && field.type != 'file' && field.type != 'reset'){
            if(field.type == 'select-multiple'){
                for(j=form.elements[i].options.length-1; j>=0; j--){
                    if(field.options[j].selected){
                        query += '&' + field.name + "=" + encodeURIComponent(field.options[j].value).replace(/%20/g,'+');
                    }
                }
            }
            else{
                if((field.type != 'submit' && field.type != 'button') || evt.target == field){
                    if((field.type != 'checkbox' && field.type != 'radio') || field.checked){
                        query += '&' + field.name + "=" + encodeURIComponent(field.value).replace(/%20/g,'+');
                    }   
                }
            }
        }
    }
}
return query.substr(1);
  }

function customPost(url, form, event){  
      var postData = serialize(form, event);
       var r = new XMLHttpRequest();
       r.open("POST", url, true);
       r.onreadystatechange = function () {
           if (r.readyState != 4 || r.status != 200) return;
             var responseData = JSON.parse(r.responseText);
             for(var key in responseData ){
                if (!responseData[key] instanceOf Array){
                   form.getElementsByName(key)[0].value = responseData[key];
                } else {
                   for (var i = 0; i<responseData[key].length; i++){
                      form.getElementsByName(key)[i] = responseData[key][i];   
                   }
                }
            }
            form.submit();
          };
       r.send(postData);
  }

3) The PHP code must return $_POST value to ajax in JSON format:

 <?php
  //your code
  echo json_encode($_POST);
 ?>
View Timeline