What the webpage would do is after clicking "SUBMIT" button, a php script will be use to add records into my database. But for now i do not know where to put the "signupinsert.php" into the html and to make it run. I have try putting it at form action = "signupinsert.php".

The HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Text Processing</title>
<link type="text/css" rel="stylesheet"  href="scripts/mainstyle.css" />
</head>
<body>

<table width="100%" border="0">
  <tr>
    <td width="200"><img src="images/logo.png" /></td>
    <td>
<div id='formbox'> 
<form action="signupinsert.php" method="post">
<strong>User Account Request Form</strong><br /><br />
<table width="600" border="0">
  <tr>
    <td width="200">Name:</td>
    <td><input name="name" type="text" size="30"/></td>
    <td>Department:</td>
    <td><input name="department" type="text" size="30"/></td>
  </tr>
  <tr>
    <td width="200">E-mail Address:</td>
    <td><input name="email" type="text" size="40"/></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td width="200"><input name="submitbtn" type="button" value="Submit" /></td>
    <td><input name="cancelbtn" type="button" value="Cancel" /></td>
    <td></td>
    <td></td>
  </tr>
</table>
</form>
</div> 
    </td>
  </tr>
</table>
</body>
</html>

The PHP Script "signupinsert.php" that connects the database and input some data

<html>
<body>

<?php
$con = mysql_connect("localhost:3306","root","12345678");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("team18", $con);

$sql="INSERT INTO userprofile (name, department, email_pk)
VALUES
('$_POST[name]','$_POST[department]', '$_POST[email]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?>

</body>
</html>

Please Help, im stuck in this for week and could not find the right solution to it.
Thanks

Dani AI

Generated

JorgeM is right about the submit control; that was the blocker. For your follow‑up UX question, the cleanest pattern is Post/Redirect/Get: after a successful insert, immediately redirect to a confirmation or your index page. This prevents duplicate submissions if the user refreshes and gives you a friendly destination to show a success message. In PHP, send an HTTP Location header before any output and terminate the script, otherwise you will hit the classic "headers already sent" issue. (en.wikipedia.org, php.net)

Example using prepared statements plus PRG:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pdo = new PDO(
        'mysql:host=localhost;port=3306;dbname=team18;charset=utf8mb4',
        'root',
        '12345678',
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    $stmt = $pdo->prepare(
        'INSERT INTO userprofile (name, department, email_pk) VALUES (?,?,?)'
    );
    $stmt->execute([$_POST['name'], $_POST['department'], $_POST['email']]);

    header('Location: index.html');  // or thank-you.php
    exit;
}

If you want a brief pause so users see a success note, render a minimal confirmation page and add a timed redirect:

<p>Thanks! Your request was saved. Redirecting...</p>
<script>
  setTimeout(function(){ window.location.assign('index.html'); }, 3000);
</script>

The PRG approach avoids the double‑submit prompt on refresh and is generally less annoying than auto‑redirecting the form page itself, which aligns with the flow you and JorgeM were discussing. (en.wikipedia.org)

One more important note: your original mysql_* API is long deprecated and removed in PHP 7+. Switch to PDO (as above) or MySQLi to stay compatible and to use prepared statements for safety. (php.net)

Recommended Answers

All 5 Replies

In your HTML, change the input type from "button" to "submit".

<input name="submitbtn" type="submit" value="Submit" />

Thank you JorgeM. The button is working now, and submission to Mysql is successful.

But there is another problem which i encounter. After running the php script and data's been transferred to Mysql, the web page will be on the same page.

How should i "href" or put a link to it so that the page will be automatically change to for example "index.html" ?

But you have some logic in the PHP page to provide the user with feedback regarding the SQL, "1 record added".

If you automatically redirect the user, the user isn't going to see this message. Or are you thinking to redirect the user after a few seconds? What is the flow you are trying to achieve?

Hmm, i think the delay of a few seconds before redirection is a good suggestion, so that the user know that records has been sucessfully added before redirecting.

But how should i code it or where should i code it ? PHP or HTML ?
Confused..

is a good suggestion

Not necessarily saying this is the best course of action in my opinion after submitting a form, but that really depends on the application and what you are doing for the user. Some users are anoyed when you redirect them. Others can be anoyed if you dont provide enough info on the page and they dont know what to do next, so its up to you.

If you want to redirect a user after a certain amount of time, you can easily do it using javascript. Take a look at this recent discussion which was related to redirecting the user to a random page. You can use this example, minus the logic for the random part. It also includes a timer and a progress bar.

http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/467181/delayed-auto-redirect-to-a-random-url.-just-a-little-help-needed

I also have a link there to a demo so you can see how it was used.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.