Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7782201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:27:59+00:00 2026-06-01T19:27:59+00:00

I designed plain html site,with one enquiry form.It has some fields like name ,address

  • 0

I designed plain html site,with one enquiry form.It has some fields like name ,address etc.
Onclick of submit button,i wanted this data to go into the database.
I created database and table using phpmyadmin on XAMPP server.

My code is as follows(hello.php):

<script>
function insert_db()
{

     <?php
     $username = $_POST['name'];
     print ($username);
     mysql_connect("localhost","root","");
     mysql_select_db("addressbook");

     mysql_query("insert into colleague values ('$username')");

     ?>


       }
 </script>

    <form  method="post">
   <input type="name" name="name" /><br />
   <input type="submit" name="mysubmit" value="Submit"  onclick="insert_db()"/>

On click of submit button ,two entries are made into the database.One is blank and other is entered value.
how to call the function only once??Is there any other way out..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:28:01+00:00Added an answer on June 1, 2026 at 7:28 pm

    You can’t mix php and javascript like this. You see, the PHP will always be executed server-side, regardless where it is in the file (PHP does not know JavaScript). Therefore, the first time you load the page, it will insert an empty row (since $_POST[‘name’] is empty). Then, if you click submit, it will do nothing, since the JavaScript code ist empty, submit the form to the server and evaluate the PHP code again, this time with the desired effect.

    A proper way to do this would be:

    <?php
    if (!empty($_POST['name'])){ // check if form values are there
        $username = $_POST['name'];
        print ($username);
        mysql_connect("localhost","root","");
        mysql_select_db("addressbook");
    
        mysql_query("insert into colleague values ('$username')");
    }
    ?>
    
    <form  method="post">
        <input type="name" name="name" /><br />
        <input type="submit" name="mysubmit" value="Submit" />
    </form>
    

    You might also want to work your way through a PHP Tutorial first

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just designed a simple For Loop using window form application. I would like
Should I use JSF2 resources instead of plain html like <link href... or url(image.png)
I have designed this gui in netBeans which has a canvas , a couple
I initially designed my site to show a login box at the top of
I'm building an application for a company that, unfortunately, has a very poorly designed
I've designed an HTML report with several div s and table s. Now my
I am having plain text file and i would like to move the content
I designed the data access portion of our framework so that every time a
I designed a report using Business Development Studio 2008 and my reporting server is
The CollapsiblePanelExtender seems primarily designed to collapse/expand things in response to user mouse events.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.