1

I have an html form and a php form.

There is a dropdown menu with different URLs. When the submit button is pressed it returns an iFrame with the URL that was from the dropdown.

My Issue: I can't get the iFrame to show the webpage. It just shows the variable.

Html Form

<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Ajax Example</title>
    <script type="text/javascript" src="jquery.js"></script>
</head>
<body>
    <ul id="info1">
        <li>Put anything in the field below.</li>
    </ul>
    <form id="form1">
        <input type="text" name="field1" id="field1">
        <input type="submit" name="submit" id="submit" value="Submit Form">
    </form>
    <strong> Site <br/> </strong>
    <select form="form1" id="MenuURL">
        <option value="google.com">Google</option>
        <option value="yahoo.com">Yahoo</option>
    </select>
    <script>
    $('#form1').submit(function(event) {
        event.preventDefault();
        $.ajax({
            type: 'POST',
            url: 'ajax.php',
            data: $(this).serialize(),
            dataType: 'json',
            success: function (data) {
                console.log(data);
                $('#info1').html(data.msg);
            }
        });
    });
    </script>
</body>
</html>

PHP Code

<?php
class ajaxValidate {
    function formValidate() {
        //Put form elements into post variables (this is where you would sanitize your data)
        $field1 = @$_POST['field1'];
        $URL = @$_POST['MenuURL'];
        //Establish values that will be returned via ajax
        $return = array();
        $return['msg'] = '';
        $return['error'] = false;
        //Begin form validation functionality
        if (!isset($field1) || empty($field1)){
            $return['error'] = true;
            $return['msg'] .= '<li>Error: Field1 is empty.</li>';
        }
        //Begin form success functionality
        if ($return['error'] === false){
            $return['msg'] = \'<li><iframe src=\" {$URL;}\" style="width:250px;height:250px;overflow:scroll;" id="MyFrame"></iframe></li>';
        }
        //Return json encoded results
        return json_encode($return);
    }
}
$ajaxValidate = new ajaxValidate;
echo $ajaxValidate->formValidate();
?>
Kevin
  • 41,694
  • 12
  • 53
  • 70
Tony
  • 121
  • 2
  • 13

1 Answers1

1

Well first of, the syntax errors:

$return['msg'] = \'<li><iframe src=\" {$URL;}\" style="width:250px;height:250px;overflow:scroll;" id="MyFrame"></iframe></li>';
                 ^                         ^

I think in google you cant just straight up use src="google.com" because Google is sending an "X-Frame-Options: SAMEORIGIN" response header you cannot simply set the src to "http://www.google.com" in a iframe.

Alternatively, you could use:

http://www.google.com/custom?q=&btnG=Search

So in your PHP:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    class ajaxValidate {
        function formValidate() {
            //Put form elements into post variables (this is where you would sanitize your data)
            $field1 = $_POST['field1'];
            $URL = $_POST['MenuURL'];
            //Establish values that will be returned via ajax
            $return = array();
            $return['msg'] = '';
            $return['error'] = false;
            //Begin form validation functionality
            if (!isset($field1) || empty($field1)){
                $return['error'] = true;
                $return['msg'] .= '<li>Error: Field1 is empty.</li>';
            }
            //Begin form success functionality
            if ($return['error'] === false){
                $return['msg'] = '<li><iframe src="'.$URL . $field1.'" style="width:250px;height:250px;overflow:scroll;" id="MyFrame"></iframe></li>';
            }
            //Return json encoded results
            return json_encode($return);
        }
    }
    $ajaxValidate = new ajaxValidate;
    echo $ajaxValidate->formValidate();
    exit;
}

In your markup and JS:

<ul id="info1">
    <li>Put anything in the field below.</li>
</ul>
<form id="form1">
    <input type="text" name="field1" id="field1">
    <input type="submit" name="submit" id="submit" value="Submit Form">
</form>
<strong> Site <br/> </strong>
<select form="form1" id="MenuURL">
    <option value="http://www.google.com/custom?btnG=Search&q=">Google</option>
    <option value="yahoo.com">Yahoo</option>
</select>
<script>
$('#form1').submit(function(event) {
    event.preventDefault();
    $.ajax({
        type: 'POST',
        url: 'ajax.php',
        data: {field1: $('#field1').val(), MenuURL: $('#MenuURL').val() },
        dataType: 'JSON',
        success: function (data) {
            console.log(data);
            $('#info1').html(data.msg);
        }
    });
});
</script>

Working Demo for google only

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • 1
    !! I was almost there!! :) Good answer tho – Awena Sep 24 '14 at 01:05
  • @Awena you could answer the yahoo part, i didn't answer it fully, i just want to make a starting point not to answer it all, but basically the idea is there, get the url and append the search string, and feed it on the `src=""` – Kevin Sep 24 '14 at 01:06
  • 1
    Well, I never thought about it. I was not aware Yahoo restrict embedding Iframes.. but I found this link usefull on Stack : http://stackoverflow.com/questions/16624919/why-iframe-dosent-work-for-yahoo-com – Awena Sep 24 '14 at 01:12
  • @Awena actually i just dug up that restriction also here on SO about google, good thing there's a workaround, on yahoo, now thats the sad part haha – Kevin Sep 24 '14 at 01:13
  • Thanks! That worked. Edit: The Value of the dropdown is not getting passed to the PHP! – Tony Sep 24 '14 at 01:14
  • @Awena yeah me too, we all do – Kevin Sep 24 '14 at 01:15
  • Thanks Ghost, it worked. But I noticed that the value of the dropdown is not getting to the PHP side... – Tony Sep 24 '14 at 01:15
  • @Tony it works on my end. If the iframe shows it means it is. About Yahoo you might want to check the links. – Awena Sep 24 '14 at 01:16
  • @Tony what do you mean `MenuURL` is not working? check the demo, its working fine – Kevin Sep 24 '14 at 01:16
  • @Ghost, My Mistake. I had copied only what I though t was changed, but you merged them. Great Job! – Tony Sep 24 '14 at 01:24
  • @Tony yes, on the demo, i just merged them so that i can show a live example, – Kevin Sep 24 '14 at 01:26