0

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?

6
  • Javascript is client-side. PHP is server-side. What you're trying to do here is not possible without some sort of bridge between the two. Commented Oct 27, 2014 at 23:02
  • How should I do it then? Clearly many websites (for instance the comment box right here) have these functions. How are they doing it? Commented Oct 27, 2014 at 23:03
  • 2
    HTTP requests (i.e. POST and GET), SESSION variables, etc. The way you go about it is up to you. Commented Oct 27, 2014 at 23:04
  • What's the easiest way? I think I'm really lazy for asking this question, but I need to make this available as soon as possible. Commented Oct 27, 2014 at 23:07
  • 1
    Unfortunately, nobody's going to be able to answer that (definitively, using factual evidence). Questions that are likely to solicit opinion are off-topic for Stack Overflow anyways. The best we can do, short of writing all the code for you is do some research, read the documentation, and decide for yourself. Alternatively, hire a professional. Commented Oct 27, 2014 at 23:08

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

"In PHP you can use the $_POST object to deal with this data." I wouldn't use such a blanket statement to answer this question, considering the default behavior for forms in HTML is GET.
I would like to point out though that POST is the right way to go about sending any data that is meant to change the server's record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.