I have two questions:
1) Say if I have a database like this:
Name Stock
Prada 10
Armani 20
Gucci 25
How do I populate each select box with say 1-10; 1-20; and 1-25
This is my code:
<?php
...
while($row = mysqli_fetch_assoc($result)){
print
"<div class='item col-xs-4 col-lg-4'>"..
"<div class='form-group'>".
"<label for='sel1'>Select list:</label>".
"<select class='form-control' id='sel1'>".
"<option>$row["Stock"]</option>".
"</select>".
"</div>".
"<button id='button" class='btn btn-success add'>Add to basket</button>".
"</div>";
}
?>
<script type="text/javascript">
$(document).on("click", ".add", function(){
// HOW DO I GET THE VALUE OF THE SELECT BOX SELECTED HERE?
});
</script>
Also, how do I get the value from the selectbox?
From what you've mentioned, it seems like you want to fetch the select box options and loop through them. In that case, only the option should be within the while loop.
And, I'd suggest you to keep your code clean and write your HTML and PHP codes separate rather than printing your HTML within PHP.
<?php /* Other PHP code */ ?>
<div class='item col-xs-4 col-lg-4'>
<div class='form-group'>".
<label for='sel1'>Select list:</label>
<select name="stock" class='form-control' id='sel1'>
<?php while($row = mysqli_fetch_assoc($result)){ ?>
<option value="<?php echo "1-".$row["Stock"]; ?>">
<?php echo "1-".$row["Stock"]; ?>
</option>
<?php } ?>
</select>
</div>
<button id='button" class='btn btn-success add'>Add to basket</button>
</div>
Hope this helps!
Related
I have set a site where I display small panels have some text on it and download button, all these data are linked from mysqli I successfully printed them but How do I Do like if button click open a link from the database depends on the button clicked,
I'm developing a shop by the way, So How do I achieve this? Here's my code also:
<?php
$query = "SELECT * FROM `combolist`";
$results = mysqli_query($db, $query);
if ($results)
{
while($row = mysqli_fetch_array($results))
{
echo "<div class='panel panel-primary'>";
echo "<div class='panel-heading'><span class='glyphicon glyphicon-list-alt'></span><b> ".$row['comboTitle']."</b> ".$row['comboDesc']."";
echo "<form>
<button type='submit' name='purchase' class='btn btn-default btn-block'>Download 2¢</button>
</form><b>".$row['addedDate']."</b> - Added Date";
echo "</div>";
echo "</div>";
}
}
?>
<?php
if (isset($_GET['purchase']))
{
$inciar_sessiono = $_SESSION["username"];
if ($credits > 2) {
$credits--;
$do_update = "UPDATE `users` SET credits='$credits' WHERE username='$inciar_sessiono'";
$results = mysqli_query($db, $do_update);
// ON SUCCESS OPEN LINK FROM DATABAWES?
echo '<script type="text/javascript">
window.open("http://google.com");
</script>';
} else {
echo " no funds";
}
}
?>
<div class="panel panel-primary">
<div class="panel-heading"><span class="glyphicon glyphicon-list-alt"></span> <b>Combolists
</b>
<form>
<button type="submit" name="purchase" class="btn btn-default btn-block">Download 2¢</button>
</form>
</div>
</div>
If I understand your question correctly you need to identify which button the user is clicking on. If that is the case a hidden form field would be a good solution.
<input type="hidden" id="yourid" name="downloadlink_id" value="2">
Then you can use the value of the hidden field, in my case "2" and fetch the value using $_GET['downloadlink_id'] in a mysqli query to get the value and use it in a mysqli query.
Like this:
SELECT comboDownloadLink FROM table_name WHERE id=2
Edit: You can get the correct id and print it in your while-loop:
echo '<form>';
echo '<input type="hidden" id="yourid" name="downloadlink_id" value=".$row["id"]">';
So I suppose you have a link in your DB. You can add link it by $row['comboDownloadLink'] . So now if you want to add it to the button you can place the button into an <a> tag like <a href='".$row['comboDownloadLink']."' target='_blank'><button ... > </a> . Now it's redirecting your user to a blank page and starting the download.
i searched previous sample questions but those are not helpful my question..
My task is i have several types of rooms in my hotel. when i select the rooms, after submit the form the room numbers and price details should be store into redirected page.
i use this command
here I modified my code like below
<?php while($row24 = mysql_fetch_array($result24)):;?>
<div class="item-gallery isotope-item bo-rad-10 hov-img-zoom triple">
<?php echo "<img src='images/$row24[pic]' alt='IMG-GALLERY'>"?>
<div class="overlay-item-gallery trans-0-4 flex-c-m">
<?php echo "<a class='btn-show-gallery flex-c-m fa fa-search' href='images/$row24[pic]' data-lightbox='gallery'></a>"?>   
<?php echo "<label class='container1 txt17'>Select Room
<input type='checkbox' name = 'check_list[]' value='$row24[roomno] ,$row24[price]'>
<span class='checkmark'></span>
</label>"?>
</div>
<div class="line-item-mainmenu text-blo3 size21 flex-col-l-m">
<?php echo "<span class='txt20 m-b-3'>" .'Room No : '
.$row24['roomno'].
"</span>"?>
<?php echo "<input type='text' class=' txt16 m-b-3'>".'Price       : '
.$row24['price']."   <i class='fa fa-rupee'></i>"."   (One Night )" ?>
</div>
</div>
<?php endwhile;?>
by using this code i get he results which i expect. but my problem is how to extract price from that array
When including object/array elements in strings, you need to wrap them with {} so they will be parsed properly.
For example
<?php echo "<img src='images/$row24[pic]' alt='IMG-GALLERY'>"?>
should be
<?php echo "<img src='images/{$row24[pic]}' alt='IMG-GALLERY'>"?>
Aside from that, it does not matter what room is selected, all these form fields (minus those unchecked input boxes) are going to be submitted and you'll have a tough time trying to parse the data out.
If, at the very least, assign roomno as an associative key for each of your fields, for example:
<input type='checkbox' name = 'check_list[]' value='{$row24[roomno]}'>
<input type='text' name='price[]' value='{$row24[roomno][price]}' />
That way, at least you can iterate through the $_POST, price field(s) based on the roomno key selection(s).
I am taking values form database in my php project. The values are taken and displayed fine. The problem is I have a button with each value. And I need a separate link for each button to see which data is selected. So that the functioning would depend on selected data. But each button gets the same name. This is my code.
<?php foreach($query as $row): ?>
<hr id="line">
<div id="wrong">
<!--<button id="connect" type="submit">Connect</button>-->
<span id="name">
<?php
if($row->fname==$this->session->userdata('catgry'))
{?>
<span class="match"> <?php echo $row->fname; ?> </span>
<?php
}
else {
echo $row->fname;
$this->session->set_userdata('re_use',$row->fname);
}
?>
 
<?php
if($row->lname==$this->session->userdata('catgry'))
{?>
<span class="match"> <?php echo $row->lname; ?> </span>
<?php
}
else {
echo $row->lname;
}
?>
</span>
<!-- <select name="back_up">
<option selected><?php echo $row->userid ?></option>
<option>a</option>
</select>-->
<button id="connect" type="submit" >Connect</button>
</div>
<?php endforeach; ?>
Please Help.
Keep a hidden input and set the value to it on click of the button. You can store the value to the radio button and use it inside the JS function.
function setSelected(elm)
{
document.getElementById('inputID').value = elm.value;
}
Outside the foreach loop.
<input type='hidden' name='SelectedUser' id='inputID' />
Inside loop, in the generated code for button.
<button id="connect" onclick='setSelected(this)' type="submit" value="<?php echo $row->userid; ?>">Connect</button>
In server side you can get the value using $_POST['SelectedUser'] when the page is submiited after selecting the user.
I have built a inquiry form on my website, the idea is instead of mailing me each time a user submits a query it is added to my database which I can then go and view via my backend system
Each query will be listed one by one with a text-area contact form below it allowing me to reply to each query individually
So far I have this (sorry it's a bit messy)
foreach ($listings as $row){
$loop.= "<h3 class='text-center'>".$row['question']."</h3>";
$loop.= "<p>".$row['message']."</p>";
$loop.= "Name: <b>".$row['name']."</b>";
$loop.= "<span class='pull-right'>Email: <b>".$row['email']."</b><br></span>";
$loop.= "<div class='clearfix'></div>";
if(isset($row['website'])){ $loop.="Website: <b>".$row['website']."</b>"; }
$loop.= "<span class='pull-right'>Date: <b>".$row['date']."</b></span>";
$loop.= "<form name='submit-response' method='POST'><fieldset>";
$loop.= "<div class='form-group'> <label for='Message".$counter."'>Your Message</label> <textarea id='Message".$counter."' name='Message".$counter."' class='form-control' rows='5'></textarea> </div>";
$loop.= "<button type='submit' name='submit".$counter."' class='btn btn-default btn-block'>Reply</button>";
$loop.= "</fieldset></form>";
}
Before that is a foreach loop and the start of the oh and $counter is set to nill
What I want is for each contact form to be unique so when I click send on one of the queries it will be sent and removed so I can send another, the only issue I am having is working out how I will work out if a submit has been hit, and which submit has been hit
The code will need to workout which button has been hit and depending on which button it will then mail() to the recipient
I'm quite stuck on this one and I'm not sure of the best course of action so any advice is really appreciated
Luke
If you click a submit button inside a <form> tag, then only that form will be submitted.
You could include a hidden field with the ID of the row in it. That way you could get rid of the $counter variables altogether.
Also if you plan on just echoing out the $loop html, I wouldn't recommend storing the HTML in a PHP variable.
<?php
foreach ($listings as $row)
{
?>
<h3 class="text-center"><?php echo $row['question']; ?></h3>
<p><?php echo $row['message']; ?></p>
Name: <b><?php echo $row['name']; ?></b>
<span class="pull-right">Email: <b><?php echo $row['email']; ?></b><br></span>
<div class="clearfix"></div>
<?php
if(isset($row['website']))
{
?>
Website: <b><?php echo $row['website']; ?></b>
<?php
}
?>
<span class="pull-right">Date: <b><?php echo $row['date']; ?></b></span>
<form action="" name="submit-response" method="POST">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<fieldset>
<div class="form-group">
<label>Your Message</label>
<textarea name="Message" class="form-control" rows="5"></textarea>
</div>
<button type="submit" class="btn btn-default btn-block">Reply</button>
</fieldset>
</form>
<?php
}
?>
add a unique id to your database table, and put it in a hidden input.
Give each form a id, and possibly each submit button a unique name. This way you can easily determine which submit button was hit, or which form was submitted, and remove it or process it via javascript.
$('form').each(function() {
$(this).submit(function(event) {
event.preventDefault();
// Add AJAX code here
$(this).remove();
});
});
Of course that was pseudo-code.
Can anybody help me on this. For showing the rezults from an MySql database i have to select school, class, subject, and exam. But listing all the classes or all the exams is not very practical so i want to do another function that when i choose some schools in the first select box it shows me in the second select box only the classes of the selected schools.
My code is:
<div id="allselects">
<form action="viewing.php" method="post" name="filt">
<div class="multsarrange">
<h1 class="choosetext" >Chose Schools</h1>
<select class="multipleselect" name="schools[]" size="8" multiple="multiple" id="shkll">
<?php
$sql = "SELECT * FROM schools ";
$scc=mysql_query($sql);
while ($db_f = mysql_fetch_assoc($scc)) {
$schcd=$db_f['schoolcode'];
$schc=$db_f['schoolname'];
echo "<option value=$schcd >$schc</option>";
}
?>
</select>
</div>
<div class="multsarrange" id="clasaajax">
<h1 class="choosetext" >Chose an Classes</h1>
<select class="multipleselect" name="classes[]" size="8" multiple="multiple" ">
<?php
$c = "SELECT * FROM classes ";
$cl=mysql_query($c);
while ($db_f = mysql_fetch_assoc($cl)) {
$clsc=$db_f['schoolID'];
$claid=$db_f['classID'];
$clay=$db_f['year'];
$clanm=$db_f['className'];
$name=schoolidton($clsc)." ".$clay." ".$clanm;
echo "<option value=$claid >$name</option>";
}
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Subjects</h1>
<select class="multipleselect" name="subjects[]" size="8" multiple="multiple">
<?php
$sb = "SELECT * FROM subjects ";
$sbi=mysql_query($sb);
while ($db_f = mysql_fetch_assoc($sbi)) {
$sbnm=$db_f['subjectName'];
$sbid=$db_f['subjectID'];
echo "<option value=$sbid >$sbnm</option>";
}
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Exams</h1>
<select class="multipleselect" name="exams[]" size="8" multiple="multiple">
<?php
$e = "SELECT * FROM exams ";
$ex=mysql_query($e);
while ($db_f = mysql_fetch_assoc($ex)) {
$id=$db_f['examID'];
$sub=$db_f['subjectID'];
$desc=$db_f['description'];
$year=$db_f['year'];
$data=$db_f['data'];
$exnam=subidton($sub)." - ".$year." - ".$desc." - ".$data;
echo "<option value=$id >$exnam</option>";
}
?>
</select>
</div>
<div id="longsubmit">
</br></br>
<input name="submit" type="submit" value="View" />
</div>
</form>
</div>
What you need to do is the following :
Setup an event listener on the select to listen for the change event - see here
Process the change event by sending the selected value to the PHP script - see here
Using PHP get the selected value and query the database as required (you already do that bit)
Send the associated HTML output or JSON or XML if you just creating a new select list - this is a simple echo
Write the output to the screen using JavaScript - this is either creating a new element based on the reply from the PHP function or inserting the HTML response
There are lots of things within this process - each with multiple options - i suggest you attempt to tackle each one and come back with specific questions if you get stuck
use ajax on abc.php get values or ids of school and make the tags you need and return back the results to the relevant id of html
function get_options(table,id_field,name_field,where_field,field_value,select_id,edit_id){
$('#'+select_id).html('<option>Loading...</option>');
$.ajax({
type: "POST", url: "abc.php", data: "&table="+table+"&id_field="+id_field+"&name_field="+name_field+"&where_field="+where_field+"&field_value="+field_value+ "&edit_id=" + edit_id +"&get_option=1",
complete: function(data){
//alert(data.responseText);
$('#'+select_id).html(data.responseText);
}
});
}
You have to use AJAX inorder to achieve this.
check this
http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php