2

I'm getting into HTML and PHP lately and there's one thing i can't wrap my head around:

What is the correct/best way to load data from a html-form into a MySQL-Database? I checked out the w3schools tutorial and multiple links but I'm still confused. They never really mention JavaScript to be necessary there but to whomever I talk always says something bout JavaScript to read data from forms. So here's how I see it:

  1. You can create a form element in your html-file and add the action="path/to/script.php and method="post/get". Then you define the different textfields, radiobuttons, e.g. that are supposedly part of the form and add a unique name attribute to each one. Then at the end you implement a button type="submit" element at the end of the form. As soon as the user will hit the submit button, all the data filled in by the user will be accessible through the superglobal variable $_GET or $_POST. All that is left is to use the php-script to set up a db connection, prepare a statment and bind parameters to it. Then I can simply access all the objects of the associative array and replace the statement wildcards with them until I got all elements.

In case the concept is correct, would it be possible to add multiple submit buttons and bind their action to different php scripts depending on which button they chose, right? Independent on it being correct or not, where should/does JavaScript come into play? How would the story look like if I were to use JavaScript. When do I have to use JavaScript to get the data using the id attribute (document.getElementById("uniqueID").value)? Is it if I want to access non-form data from withing my html-document?

I'm short on time else I'd test it myself, but I'm pretty sure someone has already done it aaand understood it as well. So thank you for answering :)

EDIT: I might've found a use case where you need JavaScript. And that's when you're trying to get the selected value from a radiobutton field. (I think) EDIT EDIT: You don't need JS to read the value of a radiobutton. Just make sure you put the name tag in the select tag or all of the radiobuttons if you don't make it into a dropdown.

Half_NO_oB
  • 35
  • 5
  • yes what your asking for is called `AJAX` to explain it might be duplicating previous answers on stackoverflow but its worth the research effort :) also if you quickly want to grasp some basic concepts of php quickly have a look at this php [series](https://youtu.be/oJbfyzaA2QA) – Bobby Axe Aug 23 '18 at 03:29
  • in the meanwhile i checked the AJAX tutorial out as well. I'll be sure to go over the series you suggested too. Only had time for the first two vids so far but they look good. – Half_NO_oB Aug 25 '18 at 00:17
  • i'm through with the tuts. They're good :) Another topic that catched my eye was [output buffering](http://web.archive.org/web/20101216035343/http://dev-tips.com/featured/output-buffering-for-web-developers-a-beginners-guide). It appears to offer some similar functionality. Not to the extent of AJAX yet very helpful especially the larger your scripts get. – Half_NO_oB Aug 31 '18 at 17:23

3 Answers3

1

Although you probably could find a way to use Javascript, I don't think you should. PHP is the way to go.

Yes you can have their actions go to different PHP pages or even stay on the same page, depending on what you're trying to do.

PHP is server-side, Javascript is client side.

An example of Javascript would be using it to change the css of an element when a button is pressed or to perform some kind of calculation.

What I would do:

  1. Set up a form page in html, style it
  2. Link that form page to a PHP page
  3. Have that PHP page connect to your DB and push the data on the form submit

But I would look into SQL injection before dealing with any confidential data.

ameijin
  • 63
  • 11
0

if you have 3 php file:"sub1.php" "sub2.php" "sub3.php"!

<div bd="sub1">click the submit1</div>
<span bd="sub2">click the submit2</span>
<input bd="sub3" value="click the submit3"/>

<button id="sub1">submit1</button>
<button id="sub2">submit2</button>
<button id="sub3">submit3</button>

According to the button clicked, the action address of the dynamic stitching access! if use jQuery:

$("button").click(function() {
    var file = $(this).attr("id") + ".php"
    var data = $("bd=['" + $(this).attr("id") + "']").text() || $("bd=['" + $(this).attr("id") + "']").val()

   $.get("host:port/" + file, {data:data}, function(d) {console.log(d)}, "json")
})
0

You never need to use JavaScript to put HTML form data in a database. You may want to use JavaScript to do things like validate that the form data is correct before submitting it or to add more rows of data to collect. Without JavaScript, you have to submit the form and then have the PHP handler send it back for more editing. JavaScript can allow you to do that work on the client side with just one submission to update the database.

In general, I would advise against relying on JavaScript for functionality. Use JavaScript to make things easier, but the form should work with no JavaScript. That way if someone is browsing without JavaScript, the form still works.

In terms of handling multiple submit buttons that cause different results, see Multiple submit buttons PHP different actions. While the accepted answer uses JavaScript, the other two answers do not. Also Two submit buttons in one form.

Another common way to use JavaScript is to use it to catch outdated browsers. So if the browser is up-to-date and can handle HTML 5, no JavaScript needed. If the browser has to do things the old way, JavaScript captures the actions and makes things happen.

As a general rule, when updating a database, you should POST the form. You use GET forms for things like searches, which aren't expected to change database state.

See Also

mdfst13
  • 630
  • 6
  • 14