Wednesday, 15 January 2014

javascript - Get value of input field inside an iframe -



javascript - Get value of input field inside an iframe -

i creating form javascript only. trying utilize javascript value of input field within iframe. possible value of field within iframe?

yes should possible, if site domain.

for example, in html page on site have iframe contents sourced website. iframe content single select field.

i need able read selected value on site. in other words, need utilize select list domain within own application. not have command on server settings.

initially hence might tempted (simplified):

html in site:

<iframe name='select_frame' src='http://www.othersite.com/select.php?initial_name=jim'></iframe> <input type='button' name='save' value='save'>

html contents of iframe (loaded select.php on domain):

<select id='select_name'> <option value='john'>john</option> <option value='jim' selected>jim</option> </select>

jquery:

$('input:button[name=save]').click(function() { var name = $('iframe[name=select_frame]').contents().find('#select_name').val(); });

however, receive javascript error when effort read value:

blocked frame origin "http://www.mysite.com" accessing frame origin "http://www.othersite.com". protocols, domains, , ports must match.

to around problem, seems can indirectly source iframe from script in own site, , have script read contents other site using method file_get_contents() or curl etc.

so, create script (for example: select_local.php in current directory) on own site contents similar this:

php content of select_local.php:

<?php $url = "http://www.othersite.com/select.php?" . $_server['query_string']; $html_select = file_get_contents($url); echo $html_select; ?>

also modify html phone call local (instead of remote) script:

<iframe name='select_frame' src='select_local.php?initial_name=jim'></iframe> <input type='button' name='save' value='save'>

now server should think loading iframe content own domain.

javascript html html5 iframe

No comments:

Post a Comment