Indeed my question is easy. when i changed "option", i must pull its id. Maybe it can work javascript. But i don't know javascript.
<h4><u> Upload Image </u></h4>
<form action="a.php" method="post" enctype="multipart/form-data">
<label style="font-size:16px;" for="selectalbum">Select Album</label>
<select style="margin-left:10px;" name="selectalbum">
<?php
$imagequery=$db->prepare("SELECT * FROM imagealbum");
$imagequery->execute(array());
$imgquery=$imagequery->fetchAll(PDO::FETCH_ASSOC);
foreach($imgquery as $imagealbum) {
?>
<option value="<?php echo $imagealbum['imagealbum_id']; ?>"><?php echo $imagealbum['imagealbum_name']; ?></option>
<?php } ?>
</select>
<br>
<input type="text" name="aaa" style="text-align:center;">
<br><br>
<input type="text" style="text-align:center;" value="<?php echo $imagealbum['imagealbum_id']; ?>">
<br><br>
<input type="file" name="files[]" value="Upload Image" />
<br>
<button style="margin-top:7px; width:90px; height:30px; background-color:green; color:#fff; border-radius:7px;" name="submit">Submit</button>
</form>
Try adding onchange event with a funcion that getId like this:
const getId = () => {
const album = document.querySelector('#albums');
const log = document.querySelector('#log');
log.innerHTML = ` Id: ${album.value}`;
}
window.onload = function() {
getId();
};
<label for="albums">Select Album:</label>
<select name="albums" id="albums" onchange="getId()">
<option value="123">Album 1</option>
<option value="321">Album 2</option>
<option value="159">Album 3</option>
</select>
<div id="log">
</div>
Related
I had to retrieve data from the database (MySQL) to drop-down list in PHP. Now, I wanna retrieve data from drop-down list into the textbox.
This is my code.
<form method="POST">
<select name="searchtitle" id="drp_dwn">
<?php
$records = $conn->query("SELECT titles from products");
if ($records->num_rows > 0) {
while ($row = $records->fetch_assoc()) {
echo "<option value=\"\"> ".$row['titles']."</option>";
$titles1 = $row['titles'];
}
}
?>
</select>
<input type="submit" class="button" value="Show">
<input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='<?php echo $row['titles']; ?>' />
</form>
I attach with images.
I hope you can support me.
Thanks
So, add an empty action attribute to the form and populate the new input with the title selected when the form has been submitted. Also change the last inputs name as searchtitle is already being used by the select and add a selected attribute to the option element:
<form action="" method="post">
<select name="searchtitle" id="drp_dwn">
<?php
$records = $conn->query("SELECT titles from products");
if ($records->num_rows > 0)
{
while ($row = $records->fetch_assoc())
{
$selected = '';
if ($row['titles'] === $_POST['searchtitle'])
{
$selected = 'selected';
}
echo "<option " . $selected . " value=\"" . $row['titles'] . "\"> " . $row['titles'] . "</option>";
}
}
?>
</select>
<input type="submit" class="button" value="Show">
<?php
if (!empty($_POST))
{
?>
<input type="text" id="textArea" class="product-new-textbox" name="searchtitle1" value="<?php echo $_POST['searchtitle']; ?>" />
<?php
}
?>
</form>
<form method="POST">
<select name="searchtitle" id="drp_dwn">
<?php
$records = $conn->query("SELECT titles from products");
if ($records->num_rows > 0) {
while ($row = $records->fetch_assoc()) {
echo "<option value=\"\"> ".$row['titles']."</option>";
$titles1 = $row['titles'];
}
}
?>
</select>
<input type="submit" id="submit" class="button" value="Show">
<input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='<?php echo $row['titles']; ?>' />
</form>
simple JS code
<script>
$(document).on("click","#submit",function(){
var x = $("#drp_dwn").val();
$("#textArea").val(x);
});
</script>
Try this. Do you want to show the dropdown value in the text box? This code will show the dropdown value in the text box on changing the dropdown value and on click the show button.
I have created a showText() function which you can call anywhere either on click the show button or on change the dropdown. You can change the dropdown option values by using your php while loop. Here is the example code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="POST">
<select name="searchtitle" id="drp_dwn" onchange="showText()">
<option value="one" >One</option>
<option value="Two" >Two</option>
<option value="Three" >Three</option>
</select>
<input type="button" onclick="showText()" class="button" value="Show">
<input type="text" id="textArea" class="product-new-textbox" name="searchtitle" value='' />
</form>
<script>
function showText()
{
$("#textArea").val($("#drp_dwn").val());
}
</script>
You can also use
<form method="POST" action="" >
<select name="searchtitle" id="drp_dwn">
<option value="one" >One</option>
<option value="Two" >Two</option>
<option value="Three" >Three</option>
</select>
<input type="submit" onclick="showText()" class="button" value="Show">
<input type="text" id="textArea" value="<?php if(isset($_POST['searchtitle'])){ echo $_POST['searchtitle']; } ?>" class="product-new-textbox" name="searchtitle" value='' />
</form>
OR you can use get method as well
<form action="" >
<select name="searchtitle" id="drp_dwn">
<option value="one" >One</option>
<option value="Two" >Two</option>
<option value="Three" >Three</option>
</select>
<input type="submit" onclick="showText()" class="button" value="Show">
<input type="text" id="textArea" value="<?php if(isset(#$_GET['searchtitle'])){ echo $_GET['searchtitle']; } ?>" class="product-new-textbox" name="searchtitle" value='' />
</form>
I have a form in html and I am saving it as search.php:
<form name="myform" action="" method="POST" onsubmit="search_clicked(); return false;">
Keyword<input type="text" name="Keyword" id="Keyword" value="XYZ" required/><!-- value is the default name that appears in the text box-->
<br>
Type
<select name="sel" id="sel" class="form-control" onchange="checkcolors(this.value)">
<option selected value="Users">Users</option>
<option value="Pages">Pages</option>
<option value="Events">Events</option>
<option value="Places">Places</option>
<option value="Groups">Groups</option>
</select>
</form>
<div id="loc_dist_displayarea" style="display:none;">
Location<input type="text" name="Location" value="90007" required/> Distance(meters)<input type="text" name="distance" value="10000" required/>
</div>
<br><br>
<input type="submit" name="Search"/>
<input type="submit" value="clear" id="clear" onclick="return clearclicked()"/>
</form>
and my php script is in the same file:
<div id="body_area" style="display:none">
<?php
echo "hi I am searching ";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
//echo "yes value is selected";
//echo $_POST["Keyword"];
if (isset($_POST['sel'])) {
$selectedval= $_POST["sel"];
echo "$selectedval";
}
//echo $_POST["Location"];
}
echo "no value is selected";
?>
</div>
I am not able to display the $_POST['sel'] while $_POST['Keyword'] is echoed.Please help.
First of all, you arent using good programming practices, you use quotation marks (these " and these ') Indiscriminately. You should only alternate between them when you have them nested.
Next, on the action paramenter you should put the name of the file, even if it's the same.
I have a pop-up form when user clicks "update", there should be a pop up form where user should be able to update but the pop up does not appear because the "a href" has "echo" in the id value. See below: echo $orderId
Update
But if I delete the <?php echo $orderId ?>part, for example Update the pop up works but echo wrong ID.
Below is the form
<div class="popup">
<form method = "post" action="">
<div>
<label for="Order ID">Order ID:</label><?php echo $orderId ?>
</div><br>
<div>
<label for="Order Date">Order Date:</label><?php echo $orderDate ?>
</div><br>
<div>
<label for="Order Total">Order Total:</label><?php echo $totalAmount ?>
</div><br>
<div>
<label for="Preparation Status">Preparation Status:</label>
<select name="preparationStatus"/>
<option value="<?php echo $preparationStatus; ?>">Default(<?php echo $preparationStatus; ?>)</option>
<option value="Pending">Pending</option>
<option value="Prepared">Prepared</option>
</select>
</div><br>
<div>
<label for="Payment Status">Payment Status:</label>
<select name="paymentStatus">
<option value="<?php echo $paymentStatus;?>">Default(<?php echo $paymentStatus;?>)</option>
<option value="Pending">Pending</option>
<option value="Paid">Paid</option>
</select>
</div><br>
<input type="submit" id="btn" name="submit" value="Save" />
<a class="close" href="#close"></a>
</form>
It only echoes id="id=1"
How can I make the popup form appear?
I need to get the text from an input in my code, and put it like a php variable in the same php file:
Input code part :
<input width="600px" type="text" name="search" id="search_box" class="search_box" placeholder="">
PHP Parameter part :
<form method="post" name="searchform" action="search/<?php echo (xyx) ?>">
How I get the input text in the (xyx) part of the code?
Complete code :
<div class="tipobusqueda" id="tipobusqueda">
<text style="color:#000">Filtrar por :</text>
<form method="post" name="searchform" action="search/<?php echo $value['Id_Cliente'] ?>">
<select id="tiposearch" name="tiposearch" style="width: 100px">
<option value="Nombre">Nombre</option>
<option value="Apellidos">Apellidos</option>
<option value="NIF">NIF</option>
<option value="Dirección">Dirección</option>
<option value="Teléfono">Teléfono</option>
<option value="Estado">Estado</option>
</select>
<input width="600px" type="text" name="search" id="search_box" class="search_box" placeholder="">
<input type="submit" value="Search" class="search_button"><!-- Filtrar texto -->
<br />
</form>
</div>
Thanks.
<html>
<head>
<script type='text/javascript' src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
</head>
<body>
<div class="tipobusqueda" id="tipobusqueda">
<text style="color:#000">Filtrar por :</text>
<form method="post" name="searchform">
<select id="tiposearch" name="tiposearch" style="width: 100px">
<option value="Nombre">Nombre</option>
<option value="Apellidos">Apellidos</option>
<option value="NIF">NIF</option>
<option value="Dirección">Dirección</option>
<option value="Teléfono">Teléfono</option>
<option value="Estado">Estado</option>
</select>
<input width="600px" type="text" name="search" id="search_box" class="search_box" placeholder="">
<input type="button" value="Search" class="search_button">
<br />
</form>
</div>
</body>
</html>
<script>
$('.search_button').click(function(){
location.href='search/'+$('#search_box').val();
});
</script>
main php file named (add_question.php)
<?php
include 'config.php';
include 'header.php';
include 'session_out.php';
?>
<?php
if(isset($_SESSION['user_type']))
{
if($_SESSION['user_type']=='Admin')
{
?>
<div class="main">
<div class="main-content">
<h1 class="pagetitle">Add Questions
<?php
$id=$_GET['sub'];
$sub=mysql_query("SELECT * FROM subject WHERE id='$id'");
while($row=mysql_fetch_array($sub))
{
echo "(".$row['subject'].")";
} ?></h1>
<button onclick="getmore();tot()">Add More</button><font color="#003333"> <- click here to add More Questions.</font>
<h4>Total
<input type="text" id='tot_q' value="1" style="width:40px" readonly> Questions
</h4>
<form action="addq.php" method="post" name="user_add">
<div class="column1-unit">
<br/>
<?php if(isset($_GET['wrnmsg1']))
{
?>
<h5 style="color:#FF0000">
<?php
echo $_GET['wrnmsg1'];
?>
</h5>
<?php
}
?>
<?php if(isset($_GET['sucmsg']))
{
?>
<h5 style="color:#003333">
<?php
echo $_GET['sucmsg'];
?>
</h5>
<?php
}
?>
<br/>
<input type="hidden" name='sub' value="<?php echo $id;?>">
<label>Question:</label><input type="text" name='question[]' style="width:150%" required>
<br/>
<br/>
Ans 1:<input type="text" name='ans1[]' style="width:40%" required>
Ans 2:<input type="text" name='ans2[]' style="width:40%" required>
<br/> <br/>
Ans 3:<input type="text" name='ans3[]' style="width:40%" required>
Ans 4:<input type="text" name='ans4[]' style="width:40%" required>
<br/>
<br/>
Currect Ans: <select name='c_ans[]' required>
<option value=""></option>
<option value="1">Ans 1</option>
<option value="2">Ans 2</option>
<option value="3">Ans 3</option>
<option value="4">Ans 4</option>
</select>
<br/>
<br/>
</div>
<div class="column1-unit" id='morediv'>
</div>
<input type="submit" value="Add Questions" name='add_sub'>
<script language="javascript" type="text/javascript">
function getXMLHTTP()
{ //function to return the xml http object
var xmlhttp=false;
try
{
xmlhttp=new XMLHttpRequest();
}
catch(e)
{
try
{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1)
{
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getmore()
{
var strURL="getmore_q.php";
var req = getXMLHTTP();
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4)
{
if (req.status == 200)
{
document.getElementById('morediv').innerHTML+=req.responseText;
}
else
{
alert("Problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
return false;
}
function tot()
{
var tot=document.getElementById("tot_q").value;
tot=parseInt(1)+parseInt(tot);
document.getElementById("tot_q").value=tot;
}
</script>
</form>
<br/>
<hr class="clear-contentunit" />
</div>
</div>
<?php }
}
else
{
?>
<script>
window.location.assign('signin.php');
</script>
<?php
}
?>
</body>
</html>
ajax call file named(getmore_q.php)
<br/>
<label>Question:</label><input type="text" name='question[]' style="width:150%" required>
<br/>
<br/>
Ans 1:<input type="text" name='ans1[]' style="width:40%" required>
Ans 2:<input type="text" name='ans2[]' style="width:40%" required>
<br/> <br/>
Ans 3:<input type="text" name='ans3[]' style="width:40%" required>
Ans 4:<input type="text" name='ans4[]' style="width:40%" required>
<br/>
<br/>
Currect Ans: <select name='c_ans[]' required>
<option value=""></option>
<option value="1">Ans 1</option>
<option value="2">Ans 2</option>
<option value="3">Ans 3</option>
<option value="4">Ans 4</option>
</select>
<br/>
<br/>
<hr class="clear-contentunit" />
now the problem is when i input text in the ajax file element that becomes blank on the next call of ajax file.ex. i add 5 question's input elements and i also put data in all that textboxes but nw if i add new question the last ajax's generated text boxes becomes empty.is there any way to solve this problem?
The problem is that you have the following line:
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
Which resets the text inside "myDiv" to be the xmlhttp.responseText.
If you want it to show "as many times as the user clicks the button," you'll have to use:
document.getElementById("myDiv").innerHTML+=xmlhttp.responseText;
Which appends the the HTML of "myDiv".