Page 1 of 1
Integrating JavaScript with PHP
Posted: Mon Dec 09, 2013 12:33 am
by rugved
Hello!
I have just started learning how to create web applications in PHP after migrating from ASP.NET.
I wanted to know how to integrate JavaScript with PHP. For example, there is a Home.php page that resides in the website's root folder. The page has an HTML button on it and a simple JavaScript function that contains code for a pop-up window that says, "Hello!" I want to call this function at the click event of this HTML button. Can anyone please help me? I know how this can be achieved in ASP.NET. I want to know how it can be done in PHP. After getting the solution to this problem, I will learn move to AJAX and then jQuery.
I am not aware whether a similar question like this has been posted on this forum before. If it has then the mods can take a call and delete this post and send me a PM containing the link to the similar question.
Thank you in advance.
Re: Integrating JavaScript with PHP
Posted: Mon Dec 09, 2013 4:02 am
by requinix
What you're describing has nothing to do with PHP. PHP doesn't get involved for simple Javascript events like that. In fact, assuming you're not including AJAX in the statement, "integrating JavaScript with PHP" is impossible.
So you have a function that presents a popup, and you want it to execute when someone clicks the button. What all do you have now? This is also a great time to start learning jQuery so a second question is what you've done/learned regarding that.
Re: Integrating JavaScript with PHP
Posted: Mon Dec 09, 2013 11:40 am
by CoursesWeb
Hi
You can generate html, and javascript code on php, if you want the html/javascript dinamically.
Smeting like this:
Code: Select all
echo '<h3>Hi ...</h3>
<script>alert("Hi ...");
</script>';
Re: Integrating JavaScript with PHP
Posted: Mon Dec 09, 2013 10:58 pm
by rugved
requinix wrote:What you're describing has nothing to do with PHP. PHP doesn't get involved for simple Javascript events like that. In fact, assuming you're not including AJAX in the statement, "integrating JavaScript with PHP" is impossible.
So you have a function that presents a popup, and you want it to execute when someone clicks the button. What all do you have now? This is also a great time to start learning jQuery so a second question is what you've done/learned regarding that.
Thank you very much for replying.
What I had meant in my question was that I have defined a button on a page, MyPage.php whose HTML code is defined as:
<input type="button" name="Button1" />
I have also created a JavaScript function in the page's <script> tag like the following:
<script type="text/javascript">
function buttonClicked() {
alert("Clicked");
}
</script>
which is inside the <head> tag. So on the click event of Button1, I want to execute the buttonClicked() JavaScript function which shows the alert box. So how do I achieve it?
Re: Integrating JavaScript with PHP
Posted: Mon Dec 09, 2013 11:48 pm
by requinix
jQuery.
The objective (in the code) is to find the button in the document and attach an event handler to its "click" event. jQuery abstracts away the majority of that work.
Code: Select all
$(function() { // this function executes when the DOM is ready
$("#popup") // find the element with id=popup (you'd have to add this to your markup)
.click(buttonClicked); // add this function as an event handler for the click event
});
There's a lot it can do with jQuery, of course, but this is a good place to start as it covers a lot of the stuff you would use it for.
Re: Integrating JavaScript with PHP
Posted: Tue Dec 10, 2013 12:24 am
by CoursesWeb
If you want to use pure javascript, to be sure that the script find the elements you want to access, just add the script at the end of html document, before </body> tag. So, the script will be processed after the other elements are loaded.
Code: Select all
<html>
<head>
<title>Some title</title>
</head>
<body>
<input type="button" name="Button1" id="btn" />
<script type="text/javascript">
function buttonClicked() {
alert("Clicked");
}
document.getElementById('btn').onclick = buttonClicked;
</script>
</body>
</html>
Re: Integrating JavaScript with PHP
Posted: Tue Dec 10, 2013 12:34 am
by requinix
Better than using onclick, which limits you to only one handler for the event and can be easily overwritten somewhere else without your knowledge, would be to use
addEventListener or
attachEvent. Which one you call depends on the browser, with that there being the reason you don't do it with vanilla Javascript by yourself.
Re: Integrating JavaScript with PHP
Posted: Wed Dec 11, 2013 1:36 am
by rugved
Thank you all very much indeed for providing me the solutions. I will try all of them and if any problems encountered, will post them in this topic itself.