Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2014-08-26
4
Medium Priority
?
1,134 Views
Last Modified: 2014-08-26
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?
0
Comment
Question by:error77
  • 3
4 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40285525
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
 

Author Comment

by:error77
ID: 40285548
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40285603
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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40285829
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question