i have one textbox and one dropdown box in each row.
now i want to enter some date in text box and select some value in dropdown.
when i click it should get saved into database.
How can i do this?
here is my code
php insert code:
When i click submit this php code should
<?php
mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
mysql_select_db("theaterdb") or die(mysql_error());
$query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname]','$_POST[language]') ") or die(mysql_error());
?>
drop down is generated dynamically
code:
function create(param) {
'use strict';
var i, target = document.getElementById('screens');
target.innerHTML = '';
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
target.innerHTML +=' '+'Language '+' ';
target.innerHTML += "<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'SELECT language FROM languages;';
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='language' id='course'>";
echo "<option>----Select Language----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['language'] ."'>" . $row['language']. "</option>";
}
echo "</select>";
?>";
target.innerHTML +='</br>';
target.innerHTML +='</br>';
}
}
the ui looks something like this...
Add name
for text box and select box like this
name="text_box"+i and name="select_box"+i. "i"
is value from counter in loop, for example if you have 100 New Moview you will have name for each text box like this text_box1, text_box2. ..... text_box100. You should on submit remeber the numbers of "New MovieS" and that is "i" and in php code just loop for each element and save them. In first iteration you will have $_POST[Fname1] and $_POST[language1], etc..
Add a line in your js which makes the inputs to print something along the lines of target.innerHTML = '<input name="RowCount" value="' + param + '" hidden />' then in your PHP use:
for ($i=0; $i < $_POST["RowCount"]; $i++) {
$query = sprintf("INSERT INTO movie ('movie_name', 'language') VALUES ('%s', '%s')",
mysqli_real_escape_string($_POST["Fname"]), // Escaping values before inserting.
mysqli_real_escape_string($_POST["language"]));
mysqli_query($con, $query);
}
in HTML Form: Set textbox and dropdown element name as same algorithm ElementName+RowNumber;
insert element for RowCount: <input type="hidden" name="RowCount" value="3">
in PHP: get values:
for($iRow=0;$iRow less $_POST['RowCount'];$iRow++) {
mysql_query("INSERT INTO movie (movie_name,language) VALUES('".$_POST['Fname'.$iRow]."','".$_POST['language'.$iRow]."');
}
Related
please i need help
i need to do specail command
when some value selected then show another dropdown box
my first dropdown box is in php
and he is my code :
$conn = mysqli_connect("localhost", "root",
"", "phoneunlock" )
or die("Cannot connect to database:" .
mysqli_connect_error($conn));
echo "<select name='selectedValue' class= 'dropdown' >";
echo '<option value="">'.'Please Select Service'.'</option>';
$sql = "SELECT id, manufacter FROM product";
$result = $conn->query($sql);
while($row = $result->fetch_assoc())
{ echo "<option value='" . $row['manufacter']. "'>" . $row['manufacter'] . '</option>';
}
echo '</select>';
and know i need some command like this :
if dropdown box selected = value then
do this
end if
sow please can any one help me
if you mean like chained combobox. try using jQuery like this :
$('#yourcomboid').on('change', function() {
var id = $(this).val();
$('#youranothercombo').empty();
$.ajax({
url: 'your-route-to-get-data/' + id,
success: function(data) {
$('#youranothercombo').empty();
for(var i = 0; i < data.length; i++) {
var item = data[i];
$('#youranothercombo').append($('<option></option>').val(item.column-name).text(item.column-name));
}
}
});
});
Hope this code works.
I have created a main drop down list and with jquery I can choose the number of drop down menus to display. Done very simply by a for loop. The main dropdown list to choose how many drop downs to display is statically populated and I am trying to dynamically populate the drop downs being displayed with data in mysql database. In the php side I am using a while loop to populate each select box. I am not getting any results being displayed. SITE
<script type="text/javascript">
$(document).ready(function () {
$('select').change(function() {
var option = $(this).val();
showFields(option);
return false;
});
function showFields(option){
var content = '';
for (var i = 1; i <= option; i++){
content += '<div id="course_'+i+'"><label>Course # '+i+'</label><br /><label>Course Name:</label> <select id="coursename_'+i+'"><option value="">--- Select ---</option>"'
<?php
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$course_query = "SELECT course_id, course_name FROM course ";
if($result = mysqli_query($mysqli, $course_query)) {
while ($idresult = mysqli_fetch_row($result))
{
$course_id = $idresult[0];
$course_name = $idresult[1];
echo'<option value="' . $course_id . '">' . $course_name . '</option>';
}
}
?>
'"';
content += '</select><br /><div><br />';
}
$('#course_catalog').html(content);
}
});
</script>
You need to echo a javascript line to start with, instead of echoing directly...
echo 'content += \'<option value="' . $course_id . '">' . $course_name . '</option>\';';
I am trying to add the dynamic drop down through javascript.
i have a dropdown which has numbers, when selected creates the drop down.
but i want to add the dynamic dropdown through javascript.
how can i do this?
here is php code
code:
<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "SELECT theater_name FROM theater;";
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
echo "<option>----Select Theater----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>";
}
echo "</select>";
?>
This php code gets the drop down values from mysql database.
but this drop down will be created dynamically from javascript
javascript code
function create(param) {
'use strict';
var i, target = document.getElementById('screens');
target.innerHTML = '';
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='Movie in hall '+i+' ';
target.innerHTML += '<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "SELECT theater_name FROM theater;";
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
echo "<option>----Select Theater----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>";
}
echo "</select>";
?>';
target.innerHTML +=' '+'Timings '+' ';
target.innerHTML += '<input type="text" name="timings">';
target.innerHTML +='</br>';
target.innerHTML +='</br>';
}
}
so now i have added this php code in javascript but it is not creating the dropdown..
Why? How can i do this
You are using PHP to generate JavaScript as the JavaScript is interpreted by the browser and PHP is processed by the server the code below will not work
target.innerHTML += '<?php'; ...
For you to understand the process is as follows,
Server reads and processes the php
The server generates the HTML from php
The JavaScript that is contained in the HTML is interpreted by the browser
what you can do is pass data to javascript, and generate the dropdown.
or if you need information from the User (as parameters) to generate the dropdown, you can make a request by ajax.
the link explain about the ajax with dropdown -> http://imasters.com.br/artigo/3918/javascript/ajax-e-php-carregando-dados-sem-refresh/
This is a string delimiter problem. You are using single quotes ' for your javascript string in
target.innerHTML += '<?php
so the string is supposed to end at the first next single quote, that is the first echoed in this line:
echo "<select name='theater_name' id='course' onchange='showUser(this.value);'>";
Javascript interpreter ends the string in ...<select name=. There is no operator or ; after that, (there is the character t) and it stucks.
You should just add a backslash \ in front of each single quote, as an escape:
echo "<select name=\'theater_name\' id=\'course\' onchange=\'showUser(this.value);\'>";
Or you can substitute the string delimiter for your echo, using the same delimiter for both javascript and php like:
echo '<select name="theater_name" id="course" onchange="showUser(this.value);">';
The same occurs in the third echo, in while.
There is some difference in PHP while using single ' or double quote " as a string delimiter: double quoted strings have more escape sequences and do expand variables. Please refer to PHP docs while making your choice. Any choice has no consequences in your script.
I am trying to print the text-box and the drop-down from a database. I am printing the text-box and drop-down based on the input given.
For example: say I am giving the input as 4. For that, it will create four text-boxes and drop-downs.
But I have the dropdown code in PHP, so now I want to print the PHP dropdown in a loop using JavaScript. How can I do this?
JavaScript code:
function create(param) {
'use strict';
var i, target = document.getElementById('screens');
target.innerHTML = '';
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
target.innerHTML +=' '+'Language '+' ';
target.innerHTML += '<input type="text" name="timings">';
target.innerHTML +='</br>';
target.innerHTML +='</br>';
}
}
PHP dropdown:
<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = "SELECT language FROM languages;";
$sth = $dbh->prepare($sql);
$sth->execute();
echo "<select name='language' id='course'>";
echo "<option>----Select Language----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" . $row['language'] ."'>" . $row['language']. "</option>";
}
echo "</select>";
?>
In place of
target.innerHTML += '<input type="text" name="timings">';
I should get a PHP dropdown instead of a textbox.
The easy way, since you have both codes....
function create(param) {
'use strict';
var i, target = document.getElementById('screens');
target.innerHTML = '';
<?php
try {
$dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$sql = 'SELECT language FROM languages;';
$sth = $dbh->prepare($sql);
$sth->execute();
$combo = "<select name='language' id='course'>";
$combo .= "<option>----Select Language----</option>";
while($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$combo .= "<option value='" . $row['language'] ."'>" . $row['language']. "</option>";
}
$combo .= "</select>";
?>
for(i = 0; i < param; i += 1) {
target.innerHTML +='</br>';
target.innerHTML +='New Movie '+i+' ';
target.innerHTML += '<input type="text" name="Fname">';
target.innerHTML +=' '+'Language '+' ';
target.innerHTML += "<?php echo $combo; ?>";
target.innerHTML +='</br>';
target.innerHTML +='</br>';
}
}
I am dynamically generating a set of radio buttons from MySQL. The buttons are creating and the variables assigned to them are populating as I did an echo print_r and it shows the array for the variable. I now want to compare the values generated from this and if the vale is "0" I want to insert a score and present a green check graphic and the word correct. If the value is "1" I want it to input different values for the score and present Incorrect and a red X graphic. Here is what I have so far (Everything populates dynamically both the question and the answers as radio buttons):
<?php
echo '<form id="frmQuestion" name="frmQuestion" method="post" action="QuizQuestion1.php">';
// Connect to the Database
require_once('mysqli_connect.php');
//create the query for the question
$q = "SELECT `Question` FROM tbl_Question WHERE QuestionID = 1";
//Create the query for the Answers
$q2 = "SELECT `Answer`,`AnswerStatusID`,`AnswerResponse` FROM tbl_Answer WHERE QuestionID = 1";
//Run the query
$r = mysqli_query($conn,$q);
//run the answer query
$r2 = mysqli_query($conn,$q2);
while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
echo '<div id="Question1"><p> ' . $row['Question'] . '</div></p>';
}
//Declare the variables as a array
$AnswerResponse = array();
$AnswerStatusID = array();
while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';
//Assign the AnswerStatusID to a var
$AnswerStatusID[] = $row2['AnswerStatusID'];
//Assign the AnswerResponse to a var
$AnswerResponse[] = $row2['AnswerResponse'];
}
//Create the submit button
echo '<input type="submit" value="Submit Answer" name="submit"/>';
echo '</form>';
//Logic for correct or incorrect answers
if (isset($_POST['q1']) && ($_POST['q1'] == '0'))
{
//create the query for the score
$q3 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('100%','1')";
//Run the query
$r = #mysqli_query ($conn,$q3);
if($r){
//Confirm message data was entered with a correct response and a graphic
echo '<h1>Correct!!</h1><img src="/images/green_Check_Low.jpg" alt="Green Check"/>';
echo 'Click here for the next question';
}
else
{
//there was an error
echo'<h1>System error</h1>';
//Debugging message
echo'<p>' . mysqli_error($conn) . '<br/><br/>Query:' . $q3 . '</p>';
}//End of nested IF
}
else{
//create the query for the score
$q4 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('0%','1')";
//Run the query
$r2 = #mysqli_query ($conn,$q3);
if($r2){
//Confirm message data was entered with a correct response and a graphic
echo '<h1>Incorrect!!</h1><img src="/images/red_X_Low.jpg" alt="Red X"/>';
echo 'Click here for the next question';
}
else
{
//there was an error
echo'<h1>System error</h1>';
//Debugging message
echo'<p>' . mysqli_error($conn) . '<br/><br/>Query:' . $q3 . '</p>';
}//End of nested IF
}
//Free up the results for the Question query
mysqli_free_result($r);
//Free up the results from the Answer query
mysqli_free_result($r2);
//close the DB connection
mysqli_close($conn);
?>
This is the answer and work as intended. Thanks for everyones input.
//Declare the variables as a array
$AnswerResponse = array();
$AnswerStatusID = array();
while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
echo '<div id="Question1"><input name="q1" type="radio" value="'.$row2['AnswerStatusID'].'"/>' . $row2['Answer'] . '</div><br/>';
//Assign the AnswerStatusID to a var
$AnswerStatusID[] = $row2['AnswerStatusID'];
//Assign the AnswerResponse to a var
$AnswerResponse[] = $row2['AnswerResponse'];
}
//Create the submit button
echo '<input type="submit" value="Submit Answer" name="submit"/>';
echo '<input type="hidden"name="submitted"value="TRUE"/>';
echo '</form>';
if($_POST['submitted']) {
//Logic for correct or incorrect answers
if (isset($_POST['q1']) && ($_POST['q1'] == '0'))
{
//create the query for the score
$q3 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('100%','1')";
//Run the query
$r3 = #mysqli_query ($conn,$q3);
//Confirm message data was entered with a correct response and a graphic
echo '<h1>Correct!!</h1><img src="/images/green_Check_Low.jpg" alt="Green Check"/>';
echo 'Click here for the next question';
}
else{
//create the query for the score
$q4 = "INSERT INTO tbl_Score (`Score`,`QuestionID`) VALUES ('0%','1')";
//Run the query
$r4 = #mysqli_query ($conn,$q4);
//Confirm message data was entered with a correct response and a graphic
echo '<h1>Incorrect!!</h1><img src="/images/red_X_Low.jpg" alt="Red X"/><br/>';
echo 'Click here to try again';
}
}