HTML+JS send form data to PHP using POST - javascript

I'm really new to all HTML/JS/PHP but I want to create a simple login page thats connected to a database. User provide username&password, once sumbit button is clicked the webpage will change to user's information page.
So far, I have my data set up and my PHP function is working fine (I tested it with POSTMAN)
, however, I cannot get my HTML/JS working. I've been using the xmlhttp method to talk with PHP but failed
So, here is my html code
`
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>login page</title>
<style type ="text/css">
h3{
text-align:center;
margin-top:20px;
}
p{
text-align:center;
}
div{
text-align:center;
}
</style>
</head>
<body>
<h3>Login Page</h3>
<form name="login">
<p>username: <input id="user" type="text" name="username" /></p>
<p>password: <input id="pass" type="password" name="password" /></p>
<p><input type="button" value ="Submit" onclick="showUser(document.getElementById('user').value, document.getElementById('pass').value)" /></p>
</form>
<br><br>
<div id="Info"><b>Student Infomation Table</b></div>
<script type="text/javascript">
function showUser(user, pass) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("Info").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","login.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("username="+user+"&password="+pass);
}
</script>
</body>
</html>
`
Here is the PHP
<?php
/*
* Checks the login
*/
include_once '/include/post_check.php';
// change header
header('Content-Type: application/json');
//check for post
if (checkPOST("username") && checkPOST("password")) {
$user_pass = $_POST['password'];
$user_name = $_POST['username'];
if ($user_pass == "" || $user_name == "") {
$temp = array("result" => "false", "reason" => "empty fields");
echo (json_encode($temp));
die();
}
} else {
$temp = array("result" => "false", "reason" => "not enough fields");
echo (json_encode($temp));
die();
}
// connect to MySQL
$con = mysqli_connect("localhost", "root", "xxx", "xxx");
// Check connection
if (mysqli_connect_errno()) {
// Print out reason
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if($user_name == "xxx" && $user_pass =="xxx"){
// write query (teacher's account)
$query = "SELECT *
FROM STUDENT";
}
$result = mysqli_query($con, $query);
echo "<table border='2'>
<tr>
<th>ID</th>
<th>Username</th>
<th>Password</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
checkPOST() function
<?php
function checkPOST($field){
return (isset($_POST[$field]));
}
?>

the ERROR is simple one. You missed the method tag and ACTION tag in your form...without method and action tag inside FORM, you cant submit your form data.
so change form tag like this,
< form name="login" method="post" action="enter php you want to submit">

use this in html
Name:
use this in php
$name=$_POST ['name'];
mysql_connect(your server);
mysql_select_db (your database);
mysql_query (insert into your database);

Related

Get ID automatically in input-field after <SELECT> without button click

I have an SQL-database with many tables. Now I would like to create an input-form to be able to get data into the db without writing the entire sql-code every time. And this should work as follows:
All table names are listed in a drop-down menu. After having selected a table name, a new table with 4 columns is created automatically:
The first column of this table simply contains an increasing number.
The second column contains the field-names of the selected table.
In the third column there are empty input fields to enter the values for the database. Only in the third line (=product name) there is a drop-down menu with all product names from the main-table of the db.
The fourth column contains the data type (e.g. int or varchar)
All tables in the database have the same structure in the first 3 columns: the first column contains the table-id, the second column the foreign-key (=master_id) and the third column the product_name.
Up to this point, the script works well with the following 2 php-files (javasql.php and getuser.php):
javasql.php:
enter code here
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="" class="optdrugs">please select</option>
<?php
include("files/zugriff.inc.php"); // database Access
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'product'";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value="'. $row['TABLE_NAME'] . '">' .
$row['TABLE_NAME']. '</option>';
echo '<br>';
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Bitte Tabelle auswählen:</b>
<br>
<?php
if (isset($_POST["submit"])) {
$sent = $_POST['sent'];
$q = $_POST['tablename'];
$column_passed = unserialize($_POST['column']); // content of array
$column is passed from getuser.php
foreach ($_POST["insertvalue"] as $key => $value) {
echo $value . "<br>";
$werte[] = "'$value'";
}
$sql="INSERT INTO $q ($column_passed) VALUES (" .
implode(", ", $werte) . ")"; // data entry
mysqli_query($db, $sql);
if (mysqli_affected_rows($db) > 0) {
echo "<h3 style='color:blue'>successful</h3>";
} else {
echo "<h3 style='color:red'>not
successful</h3>";
}
}
?>
</div>
</body>
</html>
enter code here
getuser.php:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<form id="formdatabase" name="formdatabase" action="javasql.php"
method="post">
<input type="hidden" name="sent" value="yes">
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','product');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM $q";
$result = mysqli_query($con,$sql);
$numcols = mysqli_num_fields($result); // gets number of columns in result table
$field = mysqli_fetch_fields($result); // gets the column names from the result table
$data_type_array = array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
252=>'text',
253=>'varchar',
254=>'char',
246=>'decimal'
);
$data_type_array = array_flip($data_type_array);
echo "<table>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th><th>" . 'Column names' . "</th>
<th>" . 'Values for db-entry' . "</th><th>" . 'Type' . "</th>";
echo "</tr>";
echo "<tr>";
$nr = 1;
for($x=0;$x<$numcols;$x++):?>
<td><?= $nr; ?></td>
<td><?= $field[$x]->name; ?></td>
<?= $column[] = $field[$x]->name; ?>
<td>
<?php
if ($field[$x]->name == 'Name') { // if-Beginn
?>
<select name="insertvalue[<?= $x; ?>]" id="insertvalue<?=
$x; ?>" size="1" onchange = "javascript:getSelectedRow()">
<?php
include("files/zugriff.inc.php");
$sql = "SELECT * FROM product_main ORDER BY Name";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value='. $row['Name'] . '>' .
$row['Name'] . '</option>';
echo '<br>';
}
?>
</select>
<?php
$name_option = "";
} else {
$name_option = "<input type='text' id='insertvalue" . $x . "'
name='insertvalue[" . $x . "]' size='50'>";
echo $name_option;
}
?>
</td>
<?php
$key = array_search($field[$x]->type, $data_type_array);
if($key !== false){
echo "<td>" . $key . "</td>";
}else{
echo "<td>" . $field[$x]->type . "</td>";
}
?>
<td><?= $field[$x]->type; ?></td>
<?= $nr = $nr + 1; ?>
</tr>
<?php endfor;
echo "</table>";
mysqli_close($con);
?>
<input type="hidden" name="tablename" value="<?= $q; ?>">
<input type="hidden" name="column" value="<?php echo htmlentities
(serialize($column)); ?>">
<input type="submit" value="Enter values" name="submit">
</form>
</body>
</html>
Since I need the master_id (= foreign key) in addition to the product-name for database entry, I would like to extend my script, so that the respective master_id is automatically sent to the input field in line 2, when a product-name is selected in line 3 ... without clicking a button. I tried to do this with javascript but it didn´t work. As far as I know, the solution would be to use AJAX but unfortunately, I am not very used to AJAX.
I would be more than happy, if someone could help me to solve this problem!

want to fetch conditional data using ajax

My Working scenario is, i have 4 types of Pen
1-Diamond
2-Gold
3- Bronze
4- Silver
i want when someone select Diamond Pen on the bottom input he will type quantity so if he select 1-Diamond Pen so the amount should be vary with each other, on the same way all 4 types pen rates should be vary.
My Code is
ajax.php
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<!-- <input type="text" name="users" onchange="showUser(this.value)"> -->
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Diamond</option>
<option value="2">Gold</option>
<option value="3">Bronze</option>
<option value="4">Silver</option>
</select>
<!-- <input type="submit" name="users"> -->
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>
</body>
</html>
getuser.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','fm_all');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM all_company WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['province'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
The problem is only Drop Box value is working, i do not know how to attach input quantity box with it. please help.
Note: this code is working according to id when i select drop box value so it show according to id but i did not attach it with quantity.
You can achieve this by following way in case you don't want for perform any operation on form submit
call showUser() on onkeyup for input and provide it some id may be quantity
call showUser() on onchange for select box and also provide it some id can be pen
function showUser(){
quantity = $("#quantity").val();
selected_pen = $("#pen").val();
if (quantity != null && selected_pen != null && typeof(quantity) !='undefined'){
your ajax calling code
}
}
you can call above function onchange for select box and on onkeyup in input textbox. But with above mentioned way it will call ajax every time there is change in your text field or select box.

How to update this script to respect JQuery AJAX syntax?

I have a functioning script that takes an option value, stores it in a variable q and sends an xmlhttp get call to a php script that queries a database a returns an html table with results. What I'm trying to do is update this to jQuery.ajax() with get method format ... any help is appreciated.
example_ajax_html_js.php:
<html>
<head>
<script src="//code.jquery.com/jquery-2.2.1.min.js"></script>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","/getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="Sophia">Sophia</option>
<option value="Daniel">Daniel</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
</body>
</html>
which calls getuser.php:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = $_GET['q'];
// connect to db
$sql="SELECT * FROM table WHERE first = '".$q."'";
$result = mysqli_query($web_dbi, $sql) or die("Error " . mysqli_error($web_dbi));
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>extrainfo1</th>
<th>extrainfo2</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['extrainfo1'] . "</td>";
echo "<td>" . $row['extrainfo2'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($web_dbi);
?>
</body>
</html>
Even easier than using jQuery's ajax method, I'd use the load method. Makes your job a piece of cake:
function showUser(str) {
if (str == "") {
$("#txtHint").html("");
return;
}
$("#txtHint").load("/getuser.php?q="+str);
}
That's it!
http://api.jquery.com/load/

Show loading image until dynamic content is available and new page is loaded

Here is what I need to do.
I have a php page wich generates a page like site.com/result.php?id=355 plus there is dynamic content fetched from a MySql database.
The problem: The user is redirected to that page before the content even exists in the Mysql database (it is still processed on server side). This means the user has to refresh the page few times for about 3 sec and the results are there.
I want to show a loading image while there is nothing to display until the content is in the database. Once the content can be displayed it should be printed automatically without the user having to refresh page manually.
Code :
File upload and redirect:
">
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
<input name="userfile" type="file" id="exampleInputFile">
<p class="help-block">Select File To Crypt.</p>
<!-- <input type="hidden" name="MAX_FILE_SIZE_BIND" value="10485760" />
<input name="binded" type="file" id="exampleInputFile">
<p class="help-block">Select File To Bind</p> -->
<p>
<button type="submit" name="submit" class="btn full-width btn-primary">Crypt and Scan</button>
</p>
</form>
<?php
if (!isset($_POST['submit']))
{
} else
{
mysql_connect("localhost", "scarr", "12345") or
die("Could not connect: " . mysql_error());
mysql_select_db("scar");
$uploaddir = '/var/www/html/upl/';
$_rand = generateRandomString();
$_namerand = $_rand . ".exe" ;
$uploadfile = $uploaddir .$_namerand ;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
if ($_FILES['MAX_FILE_SIZE']['size'] == 0 && $_FILES['MAX_FILE_SIZE']['error'] == 0)
{
}
$linked = "http://192.168.129.137/upl/" . $_namerand;
$sql = mysql_query("INSERT INTO Task (link, name) VALUES ('$linked', '$_rand')");
if (!$sql) {
echo 'Could not run query: ' . mysql_error();
exit;
}
?>
<form name='redirect' action='result.php?name=' method='GET'>
<input type='hidden' name='name' value='<?php echo $_rand; ?>'>
<script type='text/javascript'>
document.redirect.submit();
</script>
</form>
<?php
} else {
}
}
function generateRandomString($length = 8) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
?>
Code Result page:
<?php
mysql_connect("localhost", "scar", "12345") or
die("Could not connect: " . mysql_error());
mysql_select_db("scanner");
$namestr = $_GET["name"];
$result = mysql_query("SELECT id,name,Result FROM Results WHERE name = '$namestr'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
?>
<div id="content">
<h1><center><font color="green">{ 0/42 }</font></center></h1>
<table><thead><th>LiveGr Name</th><th>Result</th></thead>
<tr><td>LiveGridSys </td><td><font color ='Red'><?php printf($row[2])?></font></td></tr>
</table>

Pass dynamic Javascript form to PHP

I am trying to pass the value of a dynamically created JavaScript form (or pretty much just one select/option field of it) to another php file.
Here's the whole code of my request.php (which happens to use php, JavaScript and HTML):
<?php
include ("DbVerbindung.php");
?>
<!-- Verbindung zur Datenbank aufbauen -->
<?php
include "header.php";
?>
<!-- Kopfteil des Webfrontends holen -->
<!-- Hauptinhaltbereich -->
<div class="float">
<script>
<!-- dynamische Abfrage für Optionsfeld -->
function showUser(str) {
if (str=="") {
document.getElementById("gang").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("gang").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getStudiengang.php?q="+str,true);
xmlhttp.send();
}
</script>
<h2>Daten des Wählers auswählen</h2>
<table id="auswahl">
<!-- Optionen zur Abfrage der Wählerdaten -->
<form action="speichern.php" method="POST">
<tr>
<td>Fachbereich:</td>
<td id="fachbereich">
<select size="1" maxlength="20" name="fachbereich" onChange="showUser(this.value)">
<option>Fachbereich auswählen</option>
<?php $sql = "SELECT * FROM bereich";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
}
?>
</select>
</tr>
<tr>
<td>Studiengang:</td>
<td id="gang"></td>
</tr>
<tr>
<td>Geschlecht:</td>
<td id="geschlecht">
<select size="1" maxlength="20" name="geschlecht">
<?php $sql = "SELECT * FROM geschlecht";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
}
?>
</select></td>
</tr>
<tr>
<td>Name:</td>
<td id="name"><select size="1" maxlength="30" name="name" onClick="getName.p"</td>
</tr>
<tr>
<td>Wahllokal:</td>
<td id="lokal">
<select size="1" maxlength="50" name="lokal">
<?php $sql = "SELECT * FROM lokal";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
}
?>
</select></td>
</tr>
<tr>
<td id="submit">
<input type="submit" name="waehlt" value="Wähler wählt..!">
</td>
</tr>
</form>
</table>
</div>
<?php
include "footer.php";
?>
The JS script uses yet another php file -> getStudiengang.php. Here's its code:
<?php
$q = intval($_GET['q']);
include ("DbVerbindung.php");
$sql = "SELECT * FROM studiengang WHERE fs_b = '" . $q . "'";
$result = mysql_query($sql);
echo "<select size='1' name='studiengang'>";
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
}
echo "</select">;
?>
And last but not least, the php the values should get passed to (speichern.php):
if ($_POST[waehlt]) {
$uhrzeit = date('G:i:s');
echo "Wähler tritt seine Wahl an. Uhrzeit: $uhrzeit<br>";
echo "Übergebene Daten:<br>";
echo "Fachbereich: ";
$sql = "SELECT * FROM bereich where b_id = '" . $_POST[fachbereich] . "'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "$row[1]<br>";
}
echo "Studiengang: ";
echo $_POST['studiengang'];
/*$sql = "SELECT * FROM studiengang where s_id = '" . $_POST[studiengang] . "'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "$row[1]<br>";
}
*/
echo "Geschlecht: ";
$sql = "SELECT * FROM geschlecht where g_id = '" . $_POST[geschlecht] . "'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "$row[1]<br>";
}
echo "Wahllokal: ";
$sql = "SELECT * FROM lokal where l_id = '" . $_POST[lokal] . "'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "$row[1]<br>";
}
}
Note that all variables except the 'studiengang' variable (which happens to be dynamically generated) gets passed and displayed just fine.
Any help will be appreciated!
You will need to use .appendChild so the browser understands that the item is added to the form. To make the fewest modifications to your code that should still work,
replace document.getElementById("gang").innerHTML=xmlhttp.responseText; with:
var gang = document.getElementById("gang");
while (gang.firstChild) {
gang.removeChild(gang.firstChild); //clear all elements
}
var div = document.createElement('div');
/*make a div to attach the response text to
if you didn't send the select in the responseText, you could createElement('select')*/
div.innerHTML = xmlhttp.responseText;
gang.appendChild(div); //attach the select
We finally resolved the issue.
For whatever reason it was necessary to 'hardcode' the select-box in HTML already and have it not created dynamically by Javascript.
Javascript now does only dynamically generate the option-fields within the section, this does in fact solve the issue.
Latest version with changes already applied:
JavaScript in request.php:
<script>
function showUser(str) {
if (str=="") {
document.getElementById("gang").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("gang").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getStudiengang.php?q="+str,true);
xmlhttp.send();
}
</script>
Request.php (important piece of code):
<form action="save.php" method="POST">
<tr>
<td>Studiengang:</td>
<td>
<select id="gang" size="1" name="studiengang"></select>
</td>
</tr>
</form>
getStudiengang.php:
<?php
$q = intval($_GET['q']);
include ("DbVerbindung.php");
$sql = "SELECT * FROM studiengang WHERE fs_b = '" . $q . "'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
}
?>
Thanks for all the help. The answer given by serakfalcon has a good point and may be useful later on. It's not an requirement though.

Categories

Resources