Skip to content Skip to sidebar Skip to footer

IFrame With Variable URL In PHP Variable

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

Solution 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


Post a Comment for "IFrame With Variable URL In PHP Variable"