2

I have multiple submit buttons. As far as the php is concerned, they do the same thing, send input data to database. But i want to also run some javascript when specific buttons are clicked and i am having trouble.

html:

<div id="btnGroup">
 <input type="submit" id= "btn1" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn2" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn3" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn4" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn5" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn6" value="6 seats" onclick="myFunction()"/> 
</div>

js:

function myFunction(){
 alert("button clicked");
}

the function is not what i want in the end it is just something simple to test if it works.

in case this is relevant, the php associated with the buttons works fine. Once a button is clicked, i go to another page that essentially tells me that my php commands worked. Ideally I' want the javascript to run before the php.

asked Dec 12, 2017 at 13:24
4
  • 1
    But i want to also run some javascript when specific buttons are clicked-> what is some javascript mean here? . BTW you can use jQuery easily Commented Dec 12, 2017 at 13:26
  • a javascript function. my php that is associated with the buttons works fine i just want to also run a js function along with the php Commented Dec 12, 2017 at 13:28
  • If you're redirecting the user to another page then your javascript code won't run, as it's relevant to the page it resides Commented Dec 12, 2017 at 13:32
  • I was wondering that may be the case, thanks for confirming Commented Dec 12, 2017 at 13:34

5 Answers 5

1

You can send a parameter to your function which identify which button you clicked:

<script>
function myFunction(el){
 console.log("You clicked button with id "+el.id)
}
</script>
<div id= "btnGroup">
 <input type= "submit" id= "btn1" value="6 seats" onclick="myFunction(this)"/>
 <input type= "submit" id= "btn2" value="6 seats" onclick="myFunction(this)"/>
 <input type= "submit" id= "btn3" value="6 seats" onclick="myFunction(this)"/>
 <input type= "submit" id= "btn4" value="6 seats" onclick="myFunction(this)"/>
 <input type="submit" id= "btn5" value="6 seats" onclick="myFunction(this)"/>
 <input type= "submit" id= "btn6" value="6 seats" onclick="myFunction(this)"/> 
</div>

answered Dec 12, 2017 at 13:28
Sign up to request clarification or add additional context in comments.

Comments

1

You can't redirect the user to another page and run the javascript code from the first page. You have 2 options, run the JS code in that page and delay the redirection in order to handle your server code (php) or to move your JS code to the new page.

For example, you need to remove the "submit" type from your buttons and keep the JS call:

HTML

<form id="myForm">
<div id="btnGroup">
 <input type="submit" id= "btn1" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn2" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn3" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn4" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn5" value="6 seats" onclick="myFunction()"/>
 <input type="submit" id= "btn6" value="6 seats" onclick="myFunction()"/> 
</div>
</form>

JS

function myFunction(){
 alert("button clicked");
 // do your JS code here
 // this line will trigger the submit and will send you to perform your code on the server
 document.getElementById("myForm").submit();
}
answered Dec 12, 2017 at 13:35

Comments

0
$("#btn1").click(myFunction);

Simple as that. As you tagged your question with "jQuery", I assume, that you are using jQuery.

answered Dec 12, 2017 at 13:29

3 Comments

you should specify that jQuery is needed for this code to work
mrAnderson tagged his question with "jQuery".
overlooked the tag, sorry, was assuming by his code only
0

Without jquery I would do it like this:

let button = document.querySelector("#btn1"); 
button.addEventListener('click', (event) => {
 event.preventDefault(); 
 // do some code 
 window.location = "where you want to go after you run your js";
}); 

You can do it like this for every button. And don't forget to add the js file add the end of the html so it runs after the DOM has loaded.

answered Dec 12, 2017 at 13:41

Comments

0

you can use this method to run your function in your JavaScript page and run relevant function as shown below . the id of the element is written inside the brackets . '

 document.getElementById('btn1').onclick = () => {
 myFunction();
 }
 function myFunction(){
 alert("button clicked");
 };

`

answered Dec 12, 2017 at 13:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.