I've built a function that rotates a picture when you click a button. I would like to tell this function how many degrees I want it to rotate with a text field or something like that and also call a function that's being executed in php that moves a stepper motor. Is there a way to do so? I've tried with a submit button but it doesn't show the animation the way I want to.
1 Answer
Submit button are generally used to submit forms. Although you can use them too, its not recommend. You can simply use <button /> element. And you can pass the function parameter on the onclick event handler of the button.
<button id="rotate" onclick= "rotate('35')">Rotate</button>
If you want to get value of text field and send in the form.
<button id="rotate" onclick= "rotate(document.getElementById('textboxid').value)">Rotate</button>
However, calling a function being executed over PHP is a tricky part. There are different ways you can do that:
- Submit the form to the PHP page and assign URI parameters like
page.php?function=test - Direct to the PHP page with passing the values in the URI parameters similar to above.
- Use AJAX request to send and receive data.
4 Comments
Edna Ramirez
I've tried that, but how do I get the number from the text field without submitting it?
Edna Ramirez
Thank you, but I'm actually fairly new at this and I don't really know how to assign URI parameters nor use AJAX... could you please write an example with this?
Starx
@YogeshSuthar, What makes you so sure, I wrote it from your comment? It is a well know fact.