When I submit a form my information is passed into a PHP function that queries an external database and returns a set of results. When the form is completed, I have the following function run:
<script>
function transitions(){
$("#form_holder").fadeToggle();
$("#Analyzing").fadeToggle();
}
</script>
This hides the form and displays another div that indicated the query has been submitted and will return shortly. When it does return, the analysis div is supposed to hide, and the results div is supposed to appear. However, when the function actually DOES return the values, the page seems to refresh. As a result I am left with everything visible, instead of the results div only.
My best guess is this is caused by the form actually being completed and, as a result, the PHP page refreshes, which would cause the issue.
I have looked into methods of stopping php pages from refreshing on submit via AJAX, but this potential solution has proved ineffective for me.
Any ideas?
More Code
<form id="query_form" name="query_form" method="POST" onsubmit="transitions()">
<input type="text" id="query_input" name="query_input" required><br>
<input type="number" min="0" step="1" name="profit_input" id="profit_input" required><br>
<input id="send_btn" type="submit" name="send_query" value="Submit Request">
</form>
<script>
$("#query_form").ajaxForm({url: 'run.php', type: 'post',data: {query: '#query_input'},success: function(output) {
alert(output);
}});
</script>
The function in run.php
that needs to be called with an argument is called QueryData($term)
Run.php
<?php
require('RestClient.php');
$term = $_POST['qi'];
$profit = $_POST['pi'];
try {
$client = new RestClient('https://api.dataforseo.com/', null, '#########', '###########');
$post_array[] = array(
"language" => "en",
"key" => $test
);
$sv_post_result = $client->post('v2/kwrd_sv', array('data' => $post_array));
$search_volume = $sv_post_result["results"][0]['sv'];
echo "
<div id='results_div'>
<table id='results_table'>
<tr class='results_table_row'>
<td id='SEO_Search_Volume_Cell'>
$search_volume <span>Approximate Number Of Searches Per Month</span>
</td>
</tr>
</table>
";
} catch (RestClientException $e) {
echo "\n";
print "HTTP code: {$e->getHttpCode()}\n";
print "Error code: {$e->getCode()}\n";
print "Message: {$e->getMessage()}\n";
print $e->getTraceAsString();
echo "\n";
echo "An Error Has Occured, Please Try Again Later";
exit();
}
$client = null;
}
?>