troubleshooting Question

Save data of form to database with jquery, ajax and php

Avatar of joao_c
joao_cFlag for Portugal asked on
HTMLPHPjQuery
7 Comments3 Solutions2970 ViewsLast Modified:
Hello everyone!

I am working on a website which needs to collect some type of data from the visitors and then send it to a database/file.

I did an example to illustrate:

form_img
So, on the left I have a form so the users can submit their company's name and annual income. Then I have a counter that after processing the data will update the values.

What I need here is instead of the form data goes to an email, I need the data to be sent to a database or file and then the same data will update the values of the number of companies that submited the form and the accumulated annual income of all companies.


What I have so far

-the hmtl page with form and counter:

	<div id="container">


		<div id="form-submit">
			<form action="#" method="get">
				
				<label for="name-of-company">Company Name: </label>
				<input type="text" name="name-of-company" value="" id="name-of-company" /><br>
	
				<label for="annual-income">Annual Income   : </label>
				<input type="text" name="annual-income" value="" id="annual-income" /><br>
				
							
				
				<button type="submit" id="submitBtn">Submit</button>
			</form>

		</div>

		<div id="counter">
			<ul>
				<li><h4>Number of companies</h4></li>
				<li>0</li>
				<li><h4>Accumulated Annual Income</h4></li>
				<li>0</li>

			</ul>

		</div>




	</div><!--end container-->


-Using jquery/ajax to send the submitted data to a php file:

	<script>

	$('form').on('submit', function() {

		$.post('save.php', $(this).serialize(), function() {
			console.log('completed!');

		});


		return false;
	})



	</script>

-the php file(save.php):

<?php

$f = fopen('data.txt', 'w');
fwrite( $f, $_POST['name-of-company']);
fwrite( $f, $_POST['annual-income']);
fclose($f);


So, I have a simple setup (with my limited knowledge of jquery/php), that sends the data from the form to a php file that writes the data to a simple .txt file(data.txt).

I would like to ask if this aproach is relaiable and secure and how would I send the data for a database and then grab the values and update the counters with no manual editing.

Also, I would need the submitted data rendered in an html page so the admin of the site can see the submited companies and have that reference. This page needs to be secure, so only the admin can see it.

This is my first question, not sure if I am following the protocol, and if I am asking too much. Thank you and sorry for my bad english
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 3 Answers and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros