I'll try to keep this as brief as possible while still providing the required info as it is part of a much larger project. Essentially the goal of this page is to have hidden by default CSS elements that are shown as selected and need to remain visible after the "submit" action.
The question is how exactly to accomplish this. My current problem is that despite the code shown below the page refreshes after the submit button is clicked, which also resets the form visibility to hidden. I need it to remain visible while still processing the output php code.
The page is split into a left and right side, where the left side is the input (form) and the right side is the output of said form. The site is written in PHP with HTML5/JS/CSS/AJAX as necessary.
The unnecessary bits are truncated:
***** index.php *****
<?php
echo '<!DOCTYPE html>'."\n";
echo '<html>'."\n";
echo '<head>'."\n";
echo '<meta charset="UTF-8">'."\n";
echo '<script src="base.js"></script>'."\n";
...
echo '<link rel="stylesheet" type="text/css" href="input_form.css">'."\n";
...
echo '</head>'."\n";
echo '<body>'."\n";
...
echo '<span id="input_block" style="display:none;z-index:1;">';
require_once "input_form.php";
echo '</span>'."\n";
...
echo '</body>'."\n";
echo '</html>'."\n";
?>
***** base.js *****
var input_form = "";
var script_output = "";
//called from a multi-tier drop down menu of various form options
function show_sub(choice) {
var input_block = document.getElementById('input_block').style;
// Show the input form container for the first time
if(input_block.display = "none"){
input_block.display = 'block';
}
// This will hide current form to be replaced with selected form after first click
if( input_form != "" ) {
window.input_form.display = 'none';
}
// Update the current input form globally
window.input_form = document.getElementById(choice).style;
window.input_form.display = 'block';
}
//This is what is not working. Sourced from: http://stackoverflow.com/questions/23507608/form-submission-without-page-refresh
var input_data = $('#input_form');
input_data.submit(function (data) {
$.ajax({
type: input_data.attr('method'),
url: input_data.attr('action'),
data: input_data.serialize(),
success: function (complete) {
alert('data processing');
}
});
data.preventDefault();
});
***** input_form.php *****
<?php
echo '
<form id="input_form" action="index.php" method="get" name="submit">
';
require_once "cpe_input.php";
echo '
</form>
';
?>
***** cpe_input.php *****
<span id="cpe_3916">
<table>
<colgroup>
...
<tr> <!-- example form field -->
<td class="form_text">BANDWIDTH (MB)</td>
<td class="form_field" colspan="5">
<input type="text" name="bandwidth">
</td>
</tr>
<tr>
<td colspan="6">
<input type="submit" value="GENERATE SCRIPT" class="input_submit" style="position:relative;left:-1px;" onmouseover="this.style.cursor='."'pointer'".';">
</td>
</tr>
</table>
</span>
';
***** input_form.css // omitted as it is position and style options only
I didn't include the output text code as its just plain text nested in a <span>
tag to the right of the form on the page.
I also haven't yet tackled form validation, so that is a concern in terms of where in the code that step is done (ajax triggered upon leaving field focus) and preventing form submit action until all validations are passed.