Skip to content

Instantly share code, notes, and snippets.

@rushdimohamed09
Created January 17, 2017 15:40
Show Gist options
  • Save rushdimohamed09/8f3df0fa5e494de1af3cfd854d81ebf4 to your computer and use it in GitHub Desktop.
Save rushdimohamed09/8f3df0fa5e494de1af3cfd854d81ebf4 to your computer and use it in GitHub Desktop.
<?php
require_once ('../Connection/connection.php');
?>
<?php
$sql1 = "SELECT * FROM cards order by id asc";
$result1 = $conn->query($sql1);
$arr = array();
$i=0;
$count =0;
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$arr[$i] = $row1["Card"];
echo $arr[$i]; $i++;
$count++;
}
}
?>
<HTML>
<HEAD>
<TITLE>Dynamically add Textbox, Radio, Button in html Form using JavaScript</TITLE>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<SCRIPT language="javascript">
<?php
$js_array = json_encode($arr);
echo "var numbers = ". $js_array . ";\n";
?>
var option = '';
for (var i=0;i<numbers.length;i++){
option += '<option value="'+ numbers[i] + '">' + numbers[i] + '</option>';
}
$('#items').append(option);
function addInput(divName) {
var select = $("<select id='stcard[]' name='naa[]'><select/><br><br>")
$.each(numbers, function(a, b) {
select.append($("<option/>").attr("value", b).text(b));
});
$("#" + divName).append(select);
}
$(function() {
//alert('Document is ready');
$('#stcard[]').change(function() {
var sel_stud = $(this).val();
//alert('You picked: ' + sel_stud);
$.ajax({
type: "POST",
url: "test23.php",
data: 'theOption=' + sel_stud,
success: function(whatigot) {
//alert('Server-side response: ' + whatigot);
$('#LaDIV[]').html(whatigot);
} //END success fn
}); //END $.ajax
}); //END dropdown change event
}); //END document.ready
</SCRIPT>
</HEAD>
<BODY>
<H2>Dynamically add element in form.</H2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<form method="post" action="222.php">
<input type="button" value="Add" onclick="addInput('dynamicInput');" />
<input type="submit" name="submit" value="submit" />
<div id="dynamicInput"></div><div id="LaDIV[]"></div>
</form>
</BODY>
</HTML>
<?php
require_once ('../Connection/connection.php');
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
//alert('Document is ready');
$('#stcard').change(function() {
var sel_stud = $(this).val();
//alert('You picked: ' + sel_stud);
$.ajax({
type: "POST",
url: "test23.php",
data: 'theOption=' + sel_stud,
success: function(whatigot) {
//alert('Server-side response: ' + whatigot);
$('#LaDIV').html(whatigot);
} //END success fn
}); //END $.ajax
}); //END dropdown change event
}); //END document.ready
</script>
</head>
<body>
<select id="stcard" style="float:left">
<?php
$sql1 = "SELECT * FROM mstock order by id asc";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
echo "<option value=".$row1["card"].">".$row1["card"]."</option>";
}
}
?>
</select>
<div id="LaDIV"></div>
</body>
</html>
<?php
//Login to database (usually this is stored in a separate php file and included in each file where required)
$server = 'localhost'; //localhost is the usual name of the server if apache/Linux.
$login = 'root';
$pword = '';
$dbname = 'hutch';
mysql_connect($server,$login,$pword) or die($connect_error); //or die(mysql_error());
mysql_select_db($dbname) or die($connect_error);
//Get value posted in by ajax
$card = $_POST['theOption'];
//die('You sent: ' . $card);
//Run DB query
$query = "SELECT * FROM `mstock` where card ='$card'";
$result = mysql_query($query) or die('Fn test23.php ERROR: ' . mysql_error());
$num_rows_returned = mysql_num_rows($result);
//die('Query returned ' . $num_rows_returned . ' rows.');
//Prepare response html markup
$r = '&nbsp;&nbsp;&nbsp;<select>';
//Parse mysql results and create response string. Response can be an html table, a full page, or just a few characters
$qty = array();
if ($num_rows_returned > 0) {
while ($row = mysql_fetch_assoc($result)) {
$arr[0] = $row['qty'];
for ($i=1; $i<=$arr[0]; $i++){
$r = $r . '<option value="' .$i. '">' . $i . '</option>';
}
}
} else {
$r = '<p>No card in the database please click here to add a card</p>';
}
$r = $r . '</select>&nbsp;&nbsp;&nbsp;'.$arr[0];
//The response echoed below will be inserted into the
echo $r;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment