Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jquery and PHP

I have an abc.php file as a button

The code is as follows -

<button id="send-message-button" class="btn btn-xl" type="submit">Send Message</button> . I want to access this button functionality from PHP file and add jquery - when the button is clicked, I can display "submitted"

How can I do this?

Steve Hunter Steven Parker Treasure Porth

4 Answers

Steven Parker
Steven Parker
243,266 Points

:bell: Hi, I was alerted by your tag. But PHP won't be involved unless you want the "submitted" indication to come from the server after it processes the message. That topic is beyond the scope of a question answer, but it should be covered in the courses. You didn't mention which course you were on now, but I suspect that topic will be covered in ones that you will get to later.

On the other hand, jQuery and/or JavaScript can be used to show a response immediately. The exact code will depend on other factors, such as how you want the message shown, but for example this jQuery code will display the message in a pop-up box when the button is clicked:

$("#send-message-button").click(_=> alert("submitted"));

This is for my personal project. It's not related to any course. I tried this but there is no alert. I have been trying this for hours I do not know whats the problem. I made sure jquery is working but somehow I am not being able to display any message after the button is clicked.

Do you want me to share files of code? There are 3 files

Steven Parker
Steven Parker
243,266 Points

A sandbox (like codepen) or a Treehouse workspace would be ideal for sharing. For the latter, use the "snapshot" function to generate a link to share.

Steven Parker
Steven Parker
243,266 Points

In your workspace, I saw where a file "js/display.js" was being loaded, but that file (and the subfolder) did not exist in the workspace. I'm guessing that's where the code for the message would be.

But I got the popup to work just by adding "script" tags at line 110 of "form.php" and putting the code line above between them.

Steven Parker
Steven Parker
243,266 Points

Live workspace links are temporary, that one already expired. To get a persistent one, use the "snapshot" function (the camera icon).

You are a life saver. It actually worked. But there is another problem. Without inputting the information, if I click submit button, then alert pops up

Steven Parker
Steven Parker
243,266 Points

In that case, you might want to tie it to the form submit event instead of the button click event:

      $("#contact").submit(_=> alert("submitted"));

If your original question has been answered, you can mark the question solved by choosing a "best answer".
And happy coding!