question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Button onClick not working in Gatsby build

See original GitHub issue

Hi; I am planning to migrate my website from Wordpress to Gatsby. My plan is that I will use my existing Bluehost account to avoid any complications with transferring domains, especially that I will still use my Wordpress API for the back end. Accordingly, I will build my website locally using, then I will upload the “public” folder to my host folders. Now the case is that I need to add a contact form, I am planning to use ajax call from the front end to a PHP file on my server to send the email.

The issue I am facing now is that when I add the onClick prop to the button, the event fires on the development phase. However when I run gatsby build and upload the HTML files on the server, no event is fired, I inspected the <button> tag, it has no onclick event. This is my code on front end:

        <input type="text" id="fname" value="John" />
        <input type="email" id="mail" value="" />
        <input type="text" id="subject" value="" />
        <input type="text" id="content" value="" />
        <button
          onClick={() => {
            console.log("What's going on?")
            sendMail()
          }}
        >
          Send Email
        </button>

This is the mail ajax function. I tried to add it to the front end, and I also tried to add it as a script with the react Helmet component linking to an external file. In all cases, no event is fired when I upload the public folder on the host.

const sendMail = () => {
  console.log("function called from inside")
  var display = document.getElementById("firstname")
  var mail = document.getElementById("mail").value
  var name = document.getElementById("fname").value
  var content = document.getElementById("content").value

  var subject = document.getElementById("subject").value
  var xmlhttp = new XMLHttpRequest()
  xmlhttp.open("POST", "contact-form.php")
  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  xmlhttp.send(
    "fname=" +
      name +
      "&subject=" +
      subject +
      "&content=" +
      content +
      "&mail=" +
      mail
  )
  xmlhttp.onreadystatechange = function () {
    if (this.readyState === 4 && this.status === 200) {
      display.innerHTML = this.responseText
    } else {
      display.innerHTML = "Loading..."
    }
  }
}

Thanks for your support

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mkhaledchecommented, Jun 6, 2020

It worked now. The issue was with pathPrefix. Thanks @muescha

1reaction
mkhaledchecommented, Jun 6, 2020

Thanks @muescha for your reply. I’ll check the path prefix and the solution in the video which seems to be interesting. However, in my case, I don’t think I need the form tag as all the functionality I need is a button that collects some input values and sends them to the server through ajax requests. I have already tested it on a non-gatsby static file with no form tag and it worked well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Onclick events don't work on build : r/gatsbyjs - Reddit
I have a project where the onClick events on the homepage are working fine for production. But when I build out the project...
Read more >
"onClick" not working after building the project - Stack Overflow
The issue only appears after I deploy the website to a service like Netlify or Gatsby Cloud. So if I access the home...
Read more >
gatsby onclick not working的推薦與評價,GITHUB ... - 最新趨勢觀測站
Is your site not working after you've deployed to Github Pages? You see a blank page, or maybe the homepage shows up fine,...
Read more >
Gatsby: The ultimate guide with examples - LogRocket Blog
So, unlike a tool such as Next.js, Gatsby does not render anything ... After cloning this (click the Use Template button to create...
Read more >
Adding a Path Prefix - Gatsby
For example, a Gatsby blog could live at example.com… ... If this flag is not passed, Gatsby will ignore your pathPrefix and build...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found