-2

The problem i am having is i need to send the value of the username textbox to a php variable so it can be added to the SQL server.

I just can figure out how to code for that transfer of value.

I have tried a variety of way that i have found $username = $_POST['username']; however this doesn't seem to work. any thoughts would be greatly appreciated.

<!doctype html>

<html>
<head>
<meta charset="UTF-8">
<title>New Employee</title>
<style type="text/css">
body {
    background-image: url();
}
</style>
</head>

<body>

<header><img src="Border.jpg" width="100%" height="55"  alt=""/></header>
<p><img src="MLHC.png" width="620" height="205"  alt=""/>
</p>
<p>Create New Employee</p>
<p>Enter Employee Name  &nbsp;&nbsp;&nbsp;&nbsp; 
<input type="text"name="name" size="35">
</p>
<p>Select Company:  &nbsp;&nbsp;&nbsp;&nbsp;  
 <select name="company" id="Company">
    <option value="MLHC">MLHC</option>
    <option value="Placer Title Company">Placer Title Company</option>
    <option value="RMD">RMD</option>
    <option value="IRM">IRM</option>
    <option value="TTLAX">TTLAX</option>
    <option value="TTYL">TTYL</option>
    <option value="BRB">BRB</option>
    <option value="G2G">G2G</option>
    <option value="IKR">IKR</option>
  </select>
<br>
<br>
Select Supervisor:  &nbsp;&nbsp;&nbsp;&nbsp;  
   <select name="supervisor" id="Supervisor">
    <option value="John">John</option>
    <option value="Kyle">Kyle</option>
    <option value="Luke">Luke</option>
    <option value="Paul">Paul</option>
    <option value="Carrot man">Carrot man</option>
    <option value="Employee #833">Employee #833</option>
    <option value="HK-47">HK-47</option>
    <option value="Whoooooo">Whoooooo</option>
    <option value="Filler Name">Filler Name</option>
  </select>
<br>
<br>
Enter Username  &nbsp;&nbsp;&nbsp;&nbsp; <input type="text" id="username" name="username" size="35">
<br>
<br>
Enter Password &nbsp;&nbsp;&nbsp;&nbsp; <input type="text"name="password" size="35">
<br>
<br>
Enter Phone Number  &nbsp;&nbsp;&nbsp;&nbsp; <input type="text"name="phone" size="15">
<br>
<br>
<label>
  <input type="checkbox" name="admin" value="checkbox" id="admin">
  Administration Status</label>
<br>
<label>
  <input type="checkbox" name="exempt" value="checkbox" id="CheckboxGroup1_1">
  Exempt Status</label>
<br>
<p>&nbsp;</p>
<p>&nbsp;<input type="button" name="Submit" id="button" value="Submit" onClick="document.write('<?php test() ?>');">    &nbsp;&nbsp;&nbsp;
<input type="button" name="Cancel" id="button" value="Cancel" onClick="location.href='Admin.php'"></p>

<?php
function test(){





echo $username;

}

function submit(){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $name = $_POST['name'];
    $company = $_POST['company'];
    $supervisor = $_POST['supervisor'];
    $phone = $_POST['phone'];
    $admin = $_POST['admin'];
    $exempt = $_POST['exempt'];

$servername = "localhost";
$username = "abc";
$password = "123";
$dbname = "abc";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// sql to create table
$sql = "INSERT INTO users (username, password, name, company, supervisor, phone, admin, exempt)
VALUES($username, $password, $name, $company, $supervisor, $phone, $admin, $exempt)";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}

?>


</body>
</html>
5
  • Welcome to SO.SE. You should shorten your code to the minimum really necessary to demonstrate the problem. See How to Ask. Commented May 4, 2015 at 18:27
  • 1
    Your do not seem to make use of the <form> tags. Also you have written functions, yet never call them. So the code never gets executed. Commented May 4, 2015 at 18:28
  • 1
    If you are not using any form tags and your html and php code is in same page then see this if it can help you: stackoverflow.com/questions/4446727/… Commented May 4, 2015 at 18:30
  • I am not saying you shouldn't but first clear some basic syntax and working of PHP before diving into database. Commented May 4, 2015 at 18:59
  • Have a read => php.net/manual/en/tutorial.forms.php Commented May 4, 2015 at 19:39

3 Answers 3

0

put your HTML in a form as follow

<form action="#" method="post">
   <!-- your input field goes here -->
</form>

that will solve your issue if you want to make fancy js effect then use JQUERY to submit you form

Sign up to request clarification or add additional context in comments.

Comments

0

You have to enclose the whole form into these tags:

<form method="POST">...</form>

Furthermore, your submit button should look like this:

<input type="submit" value="..." ... />

Then you can access the passed values via:

$_POST["..."]

3 Comments

it is same as my answer bellow
Yes, but the submit button misses in your answer ;-) I think our answers were nearly on the same time.
fine the most important help other
0

You don't need the function stuff. Also, begin to look into SQL Injection and how to protect against it. Your code, in the wild, would be vulnerable. In your SQL, you need to also wrap your string values with single quotes.

<?php
if(isset($_POST['Submit'])){
  print_r($_POST); // Will print all the values received.

  $username = $_POST['username'];
  $password = $_POST['password'];
  $name = $_POST['name'];
  $company = $_POST['company'];
  $supervisor = $_POST['supervisor'];
  $phone = $_POST['phone'];
  $admin = $_POST['admin'];
  $exempt = $_POST['exempt'];

  $servername = "localhost";
  $username = "abc";
  $password = "123";
  $dbname = "abc";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
  } 

  // sql to create table
  $sql = "INSERT INTO users (username, password, name, company, supervisor, phone, admin, exempt)
VALUES('$username', '$password', '$name', '$company', '$supervisor', '$phone', '$admin', '$exempt')";

  if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
  } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
  }

  $conn->close();
}
?>

2 Comments

How would i pass the values from sql in php to an HTML table or textbox?
After you collect your query result set, you can echo it into the value attribute. Example: <input type="text" value="<?php echo $row['indexName']; ?>"> If my answer addressed your question, please mark it as the answer. If you have another question, I would advise searching for a tutorial or other SO threads before asking.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.