Check if a form has been submitted via iframe / cross domain (How to ?)

I know that you can get iframe values and know if a form has been submitted using javascript if both pages are on the same domain but:

Is there ANY way of doing this cross domain?
error77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You can check the HTTP_REFERER value.  You can also use PHP session values.

Can you give us an example to test with -- I'll be glad to show you how this can be done.
0
error77Author Commented:
Hi Ray, sure, here is some working code.

I have 2 pages on the same domain... index.php and form.php

In index.php I have:

<iframe id="mainiframe" src="form.php"></iframe> 


<script type="text/javascript">
  $(document).ready(function(){

     $('#mainiframe').load(function() {
        
        $(this).contents().find('#mainform').submit(function() { 
       
        
        alert('Form HAS been submitted');
        
        
        
        return true;
  });
});



  });


</script>

Open in new window


and in Form.php I have:

<?php

    if (!isset($_POST["submit"])) {
      ?>
      <form id="mainform" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
      
      Name: <input type="text" name="name"><br>
      Surname: <input type="text" name="surname"><br>
     
      <input type="submit" name="submit" value="Submit Feedback">
      
      </form>
      <?php
    } else {    

      echo 'Thank you for submitting the form';

     
   
    }

?>

Open in new window



Now, with this code, when I submit the form on the iframe my mainpage will alert me that the form in the iframe has been submitted.

Now, here comes the problem...

If I move Form.php to one of my other servers this will not work.

Is there Anyway at all around this?

Thanks
0
Ray PaseurCommented:
In form.php the action= attribute would need to point to a script on the main server.  You should be able to check this with "view source".

Let me experiment with this a bit...
0
Ray PaseurCommented:
Here are two scripts.  They are installed on my server.  You can see them in action by running the form script

The form script: http://iconoun.com/demo/temp_error77_form.php
<?php // demo/temp_error77_form.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28505517.html

// START THE SESSION AND LOAD SOME DATA
session_start();
$_SESSION['test'] = 'error77';

// CREATE A FORM FOR A POST-METHOD REQUEST
$form = <<<EOD
<form method="post" action="http://iconoun.com/demo/temp_error77_action.php">
<input type="hidden" name="thing" value="someData" />
<input type="submit"              value="Go!"      />
</form>
EOD;

echo $form;

Open in new window

The action script: http://iconoun.com/demo/temp_error77_action.php
<?php // demo/temp_error77_action.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28505517.html

// START THE SESSION IN CASE THERE IS SOME DATA
session_start();

// IF THERE IS A POST-METHOD REQUEST
if (!empty($_POST))
{
    echo 'POST DATA RECEIVED FROM ' . $_SERVER['HTTP_REFERER'] . PHP_EOL;
    var_dump($_POST);

    echo 'SESSION DATA, IF ANY: ' . PHP_EOL;
    var_dump($_SESSION);
}

Open in new window

Please install the form script on your server and run it to see what happens when we have the cross-server post request.  Once you've seen that, you can feel free to experiment with the IFrames.  I will leave this action script on my server for a while so you can test.

HTH, ~Ray
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.