Tetsudou
Tetsudou

Reputation: 224

Gather data from user, pass though PHP and use in SQL query

When the user clicks Submit button, I need to UPDATE the data in database with SQL in my PHP code but the SQL code also needs the value from form inputs in the HTML. I tried combining Javascript with the SQL code but with no luck (I knew this code probably wouldn't work but I wrote it to get the point across).

$sql = "UPDATE postsTable 
  SET ApplicationName=\"" .<script>document.getElementById("AppName").value</script>. "\" 
  WHERE PostID=3;";
$result = $conn->query($sql);

How do I get data from an HTML form into PHP?

Upvotes: 0

Views: 95

Answers (1)

OneHoopyFrood
OneHoopyFrood

Reputation: 3969

You are greatly misunderstanding the relationship between your server-side code and your client-side code.

Server-side code puts together your HTML page and ships it off to the client, you can't touch it again with server-side code. The browser then interprets it and gathers all needed resources you've included (images, scripts, etc.), then renders it. The HTML <form> allows you to gather information from the user and when they submit it the browser then sends it back to the server where you can deal with it.

In PHP you can use the $_POST, $_GET, or even $_REQUESTobjects to deal with this data. See here for POST

However this is WAY oversimplfying things. You need to understand some basics about client-server relationships. There other ways to get data and much more you need to do to make sure that what you gather stays safe (like if someone posts code). Look up some videos on client-server relationshiop and HTTP protocol.

Upvotes: 2

Related Questions