1

I have some problems when trying to pass some parameters to PHP via JS.

Here's the situation:

HTML:

<form id="numbtns" method="post" action="imphp.php">
  <button id="1" onClick="pass(this.id)">1</button>
  <button id="2" onClick="pass(this.id)">2</button>
  <button id="3" onClick="pass(this.id)">3</button>
  <button id="confirm" onClick="save()">submit</button>
</form>

JS:

pass(clicked_id) {
     var btn = document.getElementById(clicked_id);

     **// I have no idea what to do next to give the ids to the php file.**

     }

save() {
**// I want function save() to collect the parameters from function pass(), and pass      them to 'imphp.php' when submit button is clicked. I'm stuck here.**
}

PHP:

// connection part, let's skip it.

...

$num = $_POST['num'];
$query = sprintf("SELECT id, num FROM tb WHERE num = $num");

I know maybe it's a quick and stupid question for u, but I cannot figure it out for hours. So... could anyone help me out, I'd be very appreciate that.

Thx in advance.

4 Answers 4

2

I don't think you need Javascript for this. All you want to do is post the form to the PHP page, right?

<form id="numbtns" method="post" action="imphp.php">
  <input type="radio" name="num" value="1" /> 1<br />
  <input type="radio" name="num" value="2" /> 2<br />
  <input type="radio" name="num" value="3" /> 3<br />
  <input type="submit" value="Save" />
</form>

Using buttons made no sense to me at all, so I changed them to radio buttons. The user will check one, then click the Save button to submit the form.

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

3 Comments

cool, it didn't come to my mind back then... Thx for your efforts!
Rule #1: Never over-engineer it. Rule $2: Don't forget to mark your question answered.
haha, got it! And sorry for the delay. I've got some stuffs here... Cheers!
1

You need to use AJAX... i would recommend simplifying the process with a javascript library like jQuery... here is a tutorial using jQuery / AJAX / PHP: http://www.php4every1.com/tutorials/jquery-ajax-tutorial/

1 Comment

Thx for reply! I'm now working on it, appreciate what u've offered for me, but I don't think I really need to involve ajax here... First impression. I'll go further to confirm. Thx!
1

This is a very frequently asked question in one of several forms, as you can see by looking under the Related heading to the right on this page.

I see this a lot and there seems to be a fundamental flaw in many people's understanding of how PHP and Javascript work, so I wanted to throw this out there.

PHP runs on the server, executing the code in the .php page and modules there, and produces output which is sent to the browser. PHP is now done. Stopped. Fini. You PHP code is no longer running.

The web browser receives the output, builds the DOM tree, and starts rendering the HTML as a page and executing the Javascript. The Javascript can't pass parameters to the PHP because the PHP is no longer running. It has terminated execution. Kaput. In addition, the Javascript is running in the browser, on the client machine, while the PHP runs on the server.

What you can do is invoke some new PHP (or re-invoke the same PHP code to do something else) and that is usually done be making an AJAX call from Javascript, as @Jeffery A Wooden suggests in his answer.

1 Comment

thx for the very detailed explanation, and I've read it very carefully, but it seemed to be some misunderstanding there... What I'm trying to do does not expect some feedback from .php file. The process is grab some parameter from html using js, then pass it to the .php file, no feedback needed. Thx for the reply, it's the most detailed one I've seen here.
0

HTML, though I'd suggest changing the button elements to <input type="button" ...> elements

<form id="numbtns" method="post" action="imphp.php">
    <input type="hidden" name="buttonID" value="1" />
    <input type="button" onClick="pass(this)" value="1" />
    <input type="button" onClick="pass(this)" value="2" />
    <input type="button" onClick="pass(this)" value="2" />
    <input type="submit" id="confirm" value="Submit" />
</form>

Javascript:

function pass(button) {
    document.getElementById("buttonId").value = button.value;

}

Comments

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.