PHP变量中带有可变URL的iFrame

时间:2014-09-24 00:48:26

标签: php jquery html arrays ajax

我有一个html表单和一个php表单。

有一个包含不同网址的下拉菜单。 按下提交按钮后,它会返回一个iFrame,其中包含来自下拉列表的网址。

我的问题:我无法让iFrame显示网页。它只显示变量。

Html表格

<!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代码

<?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();
?>

1 个答案:

答案 0 :(得分:1)

首先,语法错误:

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

我认为在谷歌你不能直接使用src="google.com",因为谷歌正在发送一个&#34; X-Frame-Options:SAMEORIGIN&#34;响应标题你不能简单地将src设置为&#34; http://www.google.com&#34;在iframe中。

或者,您可以使用:

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

所以在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;
}

在你的标记和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