0

I have a hidden iframe where the submission of a form is handled. It goes like this:

<iframe name="foo" style="display:none;"></iframe>

So, I was wondering, if it is possible that after the stuff has happened that needs to be within the iframe, I can use javascript or something to print out data on the parent page? Thanks

EDIT: here is my form code.

<form id="bar" name="bar" method="post" target="foo" action="include/database.php">
    <input type="text" name="betamount">
    <input type='text' name="multipler">
    <input type="checkbox" name="hilo" value="High" checked>
  <input type="submit" name="submit" value="Bet">
</form>
<iframe name="foo" style="display:none;"></iframe>

Database.php handles these POST requests inside the iframe. Now, there is this one thing inside database.php which goes like this

$betamount = $_POST['betamount'];
$multiplier = $_POST['multiplier'];
$payout = (int)$betamount*(int)$multiplier;

What I want to do is, I want to use AJAX or something to echo out the 'payout' variables inside a div present on index.php

  • possible duplicate of [Iframe Function Calling From Iframe to parent page javascript function](http://stackoverflow.com/questions/2161388/iframe-function-calling-from-iframe-to-parent-page-javascript-function) – DontVoteMeDown Jun 13 '14 at 13:37
  • What have you tried? PS: an iFrame contains a separate DOM. Yes, you can access the iFrame's DOM from the parent and vice-versa, but chances are that's not what you want most of the time. Perhaps tell us what you are _really_ trying to do, and reconsider your approach. This sounds like an X-Y problem to me – Elias Van Ootegem Jun 13 '14 at 13:40
  • @EliasVanOotegem edited my main post – user3733878 Jun 13 '14 at 13:43
  • @user3733878: If you're using AJAX, why bother with an iFrame? – Elias Van Ootegem Jun 13 '14 at 14:01

1 Answers1

0

For the purposes of my answer, I'm assuming that the actions you are doing in server side cannot be replaced by a simple client-side one (using javascript).

If you are expecting a return, why don't you use AJAX directly, without iframes? Simply post the data to your php page, and return it asynchronously.

JsFiddle: http://jsfiddle.net/ericwu91/28U8n/

HTML Code:

<input type="text" id="amount" name="betamount">
<input type='text' id="multiplier" name="multipler">
<input type="checkbox" name="hilo" value="High" checked>
<button onclick="submit();return false;">Submit</button>

JS Code:

var yourData = {multiplier:$("#multiplier").val(),betamount:$("#amount").val()};

$.post("yourUrl.php",yourData,function(result){
    //Success: Use the "result" parameter to retrieve the data returned from server
    alert(result);
});

I'm using jQuery's ajax post method. Documentation here: http://api.jquery.com/jquery.post/

The perks of doing it this way is that it does exactly what you wanted to, but simplifies it by using an almost-native javascript property (asynchronous responses).

EDIT: I forgot to put the real jsfiddle link... And after I pasted all the HTML and JS code, I realized how useless the fiddle is, as it won't return any respose at all... xD

Eric Wu
  • 908
  • 12
  • 35
  • Hello thanks but this is not what I want. I want it the way I have said. It's for security purposes – user3733878 Jun 13 '14 at 14:07
  • @user3733878 , I'm sorry, but I don't understand. The only insecurity I can find when using AJAX is when you're managing secure data, such as passwords, or when there are cross-domain requests. If you still want to use it that way, you'll have to place a javascript listener in your parent window, so that it checks for response in given periods of time, say 10 milisseconds. ... Which I consider to be a waste of resource. – Eric Wu Jun 13 '14 at 19:07
  • Then consider this like passwords only. It's more like a bank application. So I need secure data. I will be getting SSL too but this is needed too, right? – user3733878 Jun 14 '14 at 05:02
  • Not necessarily. If you think your data is sensitive in means that it can't be **readed** by a malicious third party using your application, then HTTP itself is a bad route. Chrome and Firefox have a ton of developer tools to retrieve a posted data. And iframing it won't help. If you think your data is sensitive in means that they can't be **intercepted** and **modified**, then a same-domain ajax request _is as good as a post-to-iframe one_. The request/response will not leave your domain, thus will not break the SSL tunneling. – Eric Wu Jun 16 '14 at 14:17
  • same domain ajax requests? can you give me an example? @EricWu – user3733878 Jun 16 '14 at 16:04
  • Also, if I don't want them to be 'intercepted and modified' what can I do? – user3733878 Jun 16 '14 at 16:06
  • @user3733878, when I say same-domain requests/responses, I mean that your website is calling a page contained within it's same directory. That is, an Ajax script calling your very "database.php", while expecting it to return the multiplied value. My point was... If you are willing to post it into an iframe, the same result _and security level_ can be found by doing an Ajax request. Except that Javascript is designed (since it's 2.0 version, that is) to do it natively. To do such a thing using iframes, you'd have to expend a lot of time and code... again, to achieve the very same result. – Eric Wu Jun 17 '14 at 03:13