How to add dropdown dynamically - php

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.

Related

How can i post data from a select menu populated from database in to a table?

My php form here i made a drop down menu populated from a database...
<?php
include("mysql_connect.php")
?>
<html>
<head>
<link rel="stylesheet" href="form-style.css">
</head>
<body>
<form action="form_result.php" method="post">
<select name="cpu">
<?php
$result = $conn->query("SELECT * FROM cpus");
while ($row = $result->fetch_assoc()) {
echo "<option value=" . $row['CpuID'] . ">".$row['CpuManufacturer']." ".$row['CpuName']."</option>";
}
?>
</select>
Here i tried to post the selected options from the drop down menu, but the results were just showing the ID number of the database table so i tried to make a function that would echo the Name of the product for that specific ID, but it didn't work...
<?php
include("mysql_connect.php");
function select_cpu() {
$result = $conn->query("SELECT * FROM cpus WHERE CpuID=$cpu");
while ($row = $result->fetch_assoc()) {
echo "{$row['CpuManufacturer']} {$row['CpuName']}";
}
}
if(isset($_POST['submit'])) {
$cpu = $_POST['cpu'];
echo "<table>";
echo "<tr><th>You have selected:</th><tr>";
echo "
<tr><td hidden>".$cpu."</td><td>".select_cpu()."</td></tr>
";
}
echo "</table>";
?>
In the select_cpu function, you should return the string instead of echoing it:
function select_cpu() {
$result = $conn->query("SELECT * FROM cpus WHERE CpuID=$cpu");
$str = "";
while ($row = $result->fetch_assoc()) {
$str .= "{$row['CpuManufacturer']} {$row['CpuName']}";
}
return $str;
}
Your first code snippet was almost right, but if you viewed the source you'd notice the <option> code was wrong. This is because you're mistaking the double quotes shown in HTML with what PHP sees. To output a double quote in a PHP echo, either escape it ($something = "a double quote: \"") or wrap it in single quotes instead ($something = 'a double quote: "').
Try changing this line:
echo "<option value=" . $row['CpuID'] . ">".$row['CpuManufacturer']." ".$row['CpuName']."</option>";
to this:
echo '<option value="' . $row['CpuID'] . '">' . $row['CpuManufacturer'] . $row['CpuName'] . '</option>';

PHP functions with MySQL queries

I'm making simple basketball stats plugin to wordpress, and I'm using dropdown list a lot. I wanted to make function but I don't know how to pass arguments to MySQL. Here's my code:
function dropDown($tab, $option, $text){
$result = mysqli_query($con,'SELECT * FROM tab');
while($row = mysqli_fetch_array($result)){
echo "<option value=\"";
echo $row['option'] . "\">" . $row['text'];
echo "</option><br>";
}
}
and I would like to use it like this:
dropDown("team", "team_id", "name");
I tried with different quotation marks, dots etc but it doesn't seem to work.
#edit
I know PHP syntax (some of it) and I know how to use it, but I don't know how to pass $variables to MySQL query, and that's my main problem.
try
function dropDown($team, $team_id, $name) {
// use both three var where you want
$result = mysqli_query($con,'SELECT * FROM team');
echo "<select>";
while($row = mysqli_fetch($result)){
echo "<option value=\"";
echo $row['team_id'] . "\">" . $row['name'];
echo "</option>";
}
echo "</select>";
}
dropDown("team", "team_id", "name");

How to insert multiple values into database using php for loop

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]."');
}

How to print PHP dropdown in a loop using JavaScript

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>';
}
}

Passing a value through URL

Here is what i want to achieve ; sending ID's through URL's and printing it.
index.html
ID 1
ID 2
receive.php
<?php
$id_q = $_GET['id'];
print "The parameters passed through URL are $id_q";
?>
This above code works perfectly, I'm not able to do this with a list of ID's printed with a php command.
The below code is used to print all the PID's in the DB.How do i make every PID printed clickable ?
When I add html tags inside PHP code it throws up an error.
print.php
$result = mysqli_query($con,"SELECT * FROM List");
while($row = mysqli_fetch_array($result))
{
echo $row['PID'];
}
edit-query.php
$pid_q=$_GET[pid];
echo $pid_q;
while($row = mysqli_fetch_array($result))
{
echo "<a href='receive.php?id=".$row['PID']."'>".$row['PID']."</a>";
}
If you want to add your own text to a variable or echo, quote it and separate the variable with a "."
echo ''.$row['PID'].'';
you should do that like this
How about...
echo '' . $row['PID'] . '';
I believe this is what you mean?
while($row = mysqli_fetch_array($result))
{
echo 'Print ID: ' . $row['PID'] . '';
}
while($row = mysqli_fetch_array($result))
{
echo "<p id=".$row['PID']." class='clickable'>" . $row['PID'] . "</p>";
}
$(document).ready(function(){
$("#clickable").click(function(){
$(this)...something...
});
});
This is a little something you can do using JQuery if you wanted each PID to do something other than refer to another location. It will listen on any with the clickable class.

Categories