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