的iFrame在PHP变量变量网址 [英] iFrame with Variable URL in PHP Variable
问题描述
我有一个HTML表单和PHP的形式。
有不同的URL的下拉菜单。 当提交按钮是pressed返回与从下拉列表中的网址一个iFrame。
我的问题:我不能得到的iFrame来显示网页。它只是显示变量。
HTML表单
<!DOCTYPE HTML>
< HTML>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
<冠军>阿贾克斯例< /标题>
<脚本类型=文/ JavaScript的SRC =jquery.js和>< / SCRIPT>
< /头>
<身体GT;
&所述;微升的id =INFO1>
<李>放任何东西在下面的字段< /李>
< / UL>
<表格ID =Form1的>
<输入类型=文本名称=字段1ID =字段1>
<输入类型=提交名称=提交ID =提交值=提交表单>
< /形式GT;
<强>网站< BR /> < / STRONG>
<选择表=Form1的ID =MenuURL>
<期权价值=google.com>谷歌< /选项>
<期权价值=yahoo.com>雅虎< /选项>
< /选择>
<脚本>
$('#Form1的)。递交(函数(事件){
。事件preventDefault();
$阿贾克斯({
键入:POST,
网址:'ajax.php,
数据:$(本).serialize()
数据类型:JSON,
成功:功能(数据){
的console.log(数据);
$('#INFO1)HTML(data.msg);
}
});
});
< / SCRIPT>
< /身体GT;
< / HTML>
PHP code
< PHP
类ajaxValidate {
功能formValidate(){
//把表单元素融入后的变量(这是在那里你会净化你的数据)
$字段1 = @ $ _ POST ['字段1'];
$ URL = @ $ _ POST ['MenuURL'];
//建立,将通过AJAX返回值
$收益率=阵列();
$返回['味精'] ='';
$返回['错误'] = FALSE;
//开始表单验证功能
如果(!使用isset($字段1)||空($字段1)){
$返回['错误'] =真;
$返回['味精'] ='<李>错误:字段1是空的< /李>。
}
//开始的形式成功的功能
如果($返回['错误'] === FALSE){
$返回['味精'] = \'<李>< IFRAME SRC = \{$ URL;} \的风格=宽度:250像素,高度:250像素;溢出:滚动; ID =MyFrame>< / IFRAME>< /李>';
}
//返回JSON EN codeD结果
返回json_en code($返程);
}
}
$ ajaxValidate =新ajaxValidate;
回声$ ajaxValidate-> formValidate();
?>
那么首先的,语法错误:
$返回['味精'] = \'<李>< IFRAME SRC = \{$ URL;} \的风格=宽度:250像素,高度:250像素;溢出:滚动; ID =MyFrame>< / IFRAME>< /李>';
^ ^
我觉得在谷歌,你不能只是直上使用 SRC =google.com
,因为谷歌正在发送X帧选项:SAMEORIGIN响应头你不能简单地设置SRC为 http://www.google.com 在一个iframe中。
另外,你可以使用:
http://www.google.com/custom?q=&btnG=Search
所以,在你的PHP:
如果($ _ SERVER ['REQUEST_METHOD'] =='POST'){
类ajaxValidate {
功能formValidate(){
//把表单元素融入后的变量(这是在那里你会净化你的数据)
$字段1 = $ _ POST ['字段1'];
$ URL = $ _ POST ['MenuURL'];
//建立,将通过AJAX返回值
$收益率=阵列();
$返回['味精'] ='';
$返回['错误'] = FALSE;
//开始表单验证功能
如果(!使用isset($字段1)||空($字段1)){
$返回['错误'] =真;
$返回['味精'] ='<李>错误:字段1是空的< /李>。
}
//开始的形式成功的功能
如果($返回['错误'] === FALSE){
$返回['味精'] ='<李>< IFRAME SRC ='$ URL $ field1的'。的风格=宽度:250像素,高度:250像素;溢出:滚动; ID =MyFrame>< / IFRAME>< /李>';
}
//返回JSON EN codeD结果
返回json_en code($返程);
}
}
$ ajaxValidate =新ajaxValidate;
回声$ ajaxValidate-> formValidate();
出口;
}
在您的标记和JS:
< UL ID =INFO1>
<李>放任何东西在下面的字段< /李>
< / UL>
<表格ID =Form1的>
<输入类型=文本名称=字段1ID =字段1>
<输入类型=提交名称=提交ID =提交值=提交表单>
< /形式GT;
<强>网站< BR /> < / STRONG>
<选择表=Form1的ID =MenuURL>
<期权价值=http://www.google.com/custom?btnG=Search&q=>谷歌< /选项>
<期权价值=yahoo.com>雅虎< /选项>
< /选择>
<脚本>
$('#Form1的)。递交(函数(事件){
。事件preventDefault();
$阿贾克斯({
键入:POST,
网址:'ajax.php,
数据:{字段1:$('#字段1)VAL(),MenuURL:$('#MenuURL)VAL()},
数据类型:JSON,
成功:功能(数据){
的console.log(数据);
$('#INFO1)HTML(data.msg);
}
});
});
< / SCRIPT>
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();
?>
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>
这篇关于的iFrame在PHP变量变量网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!