Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Shams Nelson
Shams Nelson
2,888 Points

Can I use "onclick" to send info to mySQL?

Sorry if this is in the wrong section, but as far as I understand (and please correct me if I'm wrong) php is used to communicate with a database server like mySQL...

My question is, can i use onclick to send info from a survey to a database? Or is there another/better way to do this?

<form>
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female<br>
<input type="submit" onclick=(reference to PHP code)>
</form> 

5 Answers

Andrew Showalter
Andrew Showalter
14,028 Points

You can, but it is not that simple and not by using the onClick method. It also depends on what you are trying to accomplish. My recommendation would be to just post the form to a php script using in the action property of the form.

Or, if you are trying to do something without a new page load, you can use AJAX. You can read more about AJAX with jQuery here.

Shams Nelson
Shams Nelson
2,888 Points

Thanks for your reply Andrew.

Well I'd like the "submit" button to send the user to a different page depending on their selection. And I'd also like the data sent to mySQL.

Pardon me, for I'm a noob, but are you saying to just create the form and everything within the PHP script?

Andrew Showalter
Andrew Showalter
14,028 Points

It would look more like this....

<form action="pathto/script.php" method="get">
<input type="radio" name="sex" value="male">Male<br>
<input type="radio" name="sex" value="female">Female<br>
<input type="submit" value="Submit!">
</form>

Keep in mind that it doesn't have to be php, it can be your flavor of choice for server side scripting languages. But, php would probably be a good solution for what you are trying to do since it comes pre-installed on web servers.

what happens after that, is in the php file where you are posting the form, you would have to write some sort of control structure to handle your mysql functionality as well as redirecting to the proper page based on the user input.

On that pathto/script.php, you would have to grab the variables using either $_GET or $_POST super globals. You could then use those values to updates your database and then redirect based on the user input. But, in order to post to a database, you will have to set up a connection with the database as well as be able to write the SQL for saving the record.

I would suggest going through the php fundamentals course here and then this will become much more clear.

AJAX is another possibility, but, is much more advanced than posting to a simple php script.

Shams Nelson
Shams Nelson
2,888 Points

I think I get what you're saying here Andrew. Yea I need to keep working on that PHP track, and check back here and I'm sure it'll be much clearer. Good looking out man, thanks.

Andrew Showalter
Andrew Showalter
14,028 Points

Yeah and just to expand a little bit more for you. The name value pairs that get submitted with the form from the inputs will be available to you as super globals $_GET or $_POST depending on the method you use in the form submission. For example, if the selection was the female radio button, on the script where you post the form to you could capture those values like this

<?php 
$user_sex = $_GET['sex'] // this value was passed from the form with the input name="sex";
?>

After that, you can updated your database and then write the re-direct to send them where you want to go based on that input. I hope that helps!