So I have an array of default values in a php array the file (backend.php) has multiple functions and acts. I want to target just the array with the act="default". I need to take the the encoded json php array and populate and html form with it.
html page - frontend.html
<html>
<head>
</head>
<body>
<h1>Form Validation</h1>
<form id="PersonForm">
Name:
<input id="name" type ="text" name="name"></input>
<br><br>
Postal Code:
<input id="postal" type ="text" name="postal"></input>
<br><br>
Phone Number:
<input id="phone" type ="text" name="phone"></input>
<br><br>
Address:
<input id="address" type ="text" name="address"></input>
<br><br>
<input type="submit"></input>
</form>
<div id= "content"></div>
<a href="frontend.html">Refresh</a>
<a id="InsertDefault" href="#">Insert Default Data</a>
<br><br>
<ul id="errors"></ul>
<p id="success"></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="main.js" type="text/javascript"></script>
</body>
</html>
php page - backend.php
<?php
if ($_REQUEST['act'] == 'default'){
$defaultData = array(
'name' => "Jane",
'postal' => "L5B4G6",
'phone' => "9055751212",
'address' => "135 Fennel Street"
);
echo json_encode($defaultData);
}
else if...
?>
main.js page (errors here)
$(document).ready(function()
{
$("#InsertDefault").click(function()
{
// make an AJAX call here, and place results into the form
$(document).ready(function(){
$('#content').load('backend.php', {'default':'defaultData'}, //this format
function (){ $('#content').html('these are the default values')}
);
// prevents link click default behaviour
defaultData.preventDefault();
return false;
});
$("#PersonForm").submit(function()
{
// Clear any success or error messages
$("#success").html("");
$("#errors").empty();
// make an AJAX call here, and set error or success accordingly
// prevents submit button default behaviour
return false;
});
});
javascriptblock has to be re-written, you have adocument.readyinside aclickhandler inside thedocument.ready. Also the formsubmithandler is inside the click handler.