Im wondering what is the best way to display an image after a user select one of the categories on the selection:
<form method="post" action="../user/posting.php">
<input type="text" name="title" required placeholder="Title"/>
<textarea name="body" rows="6" cols="50" placeholder="Details....."></textarea>
//Here is where the selection of the icon is
<select name="stat_icon" required>
<option name="fire" value="fire">Fire</option>
<option name="traffic" value="traffic">Traffic</option>
<option name="construction" value="construction">Construction</option>
<option name="crash" value="crash">Crash</option>
<option name="weather" value="weather">Weather</option>
<option name="robbery" value="robbery">Robbery</option>
<option name="deviation" value="deviation">Deviation</option>
<option name="police" value="police">Police</option>
</select>
<input type="text" name="location" required placeholder="Location">
<input type="file" name="img_stat">
<input type="submit" value="Stat!" />
</form>
I don't know what is the best way to display the image, if I have to upload the image first or I have to only set some if statements to select the image is in one of my folders. Im using php, html and for database mysql. Any suggestion
you can display an image using JQuery, ofcourse you have to upload all images to your images directory first.
$(document).ready(function() {
$("#imageSelector").change(function() {
var src = $(this).val();
$("#imagePreview").html(src ? "<img src='path/to/your/image/" + src + ".jpg'>" : "");
});
});
<select id="imageSelector" name="stat_icon" required>
<option name="fire" value="fire">Fire</option>
<option name="traffic" value="traffic">Traffic</option>
<option name="construction" value="construction">Construction</option>
<option name="crash" value="crash">Crash</option>
<option name="weather" value="weather">Weather</option>
<option name="robbery" value="robbery">Robbery</option>
<option name="deviation" value="deviation">Deviation</option>
<option name="police" value="police">Police</option>
</select>
<div id="imagePreview"></div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
Ok so basically you should do this ( This suggestion is just an opinion form me , its a way that i use it ) :
You should have your imgs already saved in the database ( a name for the img so you can grape it from the folder ).
When the user pick a catalog from the forms you should have a php code that take the picked data and bring the data that compares with the user choice . i mean by the data is the Imgs .
This is an example from a code that i already have on my website , its easy , i used PDO instead of MYSQLI :
<form action="" method="post">
<select name="catalog" class="form-control input-lg" tabindex="1" required>
<option selected>Pick Your Favorite Catalog</option>
<?php // Fetching the catalogs names from the database
while($FCN = $FetchCatalogsName->FETCH(PDO::FETCH_ASSOC))
{
?><option value="<?php echo $FCN['catalog'];?>"><?php echo $FCN['catalog'];?></option><?php
}
?>
</select>
<br>
<input type="submit" name="filter" class="col-sm-4 form-control input-lg" value="Show Catalog">
</form>
Now we have to create the php code that will recive the img catalog from
<?php
include_once('lib/dbconnect.php');
if(isset($_POST['filter']))
{
$catalog = $_POST['catalog'];
$FetchImgs = $conn->prepare("SELECT * FROM picture WHERE catalog = ? "); // Fetching all Imgs that related to that catalog that the user has picked .
$FetchImgs->execute(array($catalog));
if($FetchImgs)
{
?>
<?php while($FI = $FetchImgs->fetch(PDO::FETCH_ASSOC))
{
?>
<div class="col-md-3">
<img src="uploading/<?php echo $FI['Filename']; ?>"> // $FI['Filename'] this will return the name of the img from the database .
</div>
<?php }
}
else
{
echo "something went wrong ";
}
}
?>
Note : in my case the php code is in the same page as the html form and its directly under it .
I don't know what is the best way to display the image, if I have to upload the image first or I have to only set some if statements to select the image is in one of my folders.
If your sole purpose is to display the image based on the selection, then there's no need to upload any images. The only catch here is, your image names should correspond to value attributes of select input. Also, image extension should be consistent.
So first of all, remove this line <input type="file" name="img_stat"> from your form and after form submission, display the appropriate image like this:
$image_path = '/yourImagePath/' . $_POST['stat_icon'] . '.jpg';
echo '<img src="' . $image_path . '" alt="stat_icon" />';
Note(s):
Change .jpg as per your image extensions
Change /yourImagePath/ and give the appropriate directory path
Related
I am passing a variable from page1 to page2 using GET and it's already working. However, I also use another get in the <select> <option> on page2 which means when I click an item in the option, the value I get from page1 disappears and becomes undefined since the address bar changes. I want the variable I get from page1 to be compared on SQL where clause. I can't think of a way on how to solve this, I'm not really knowledgeable in PHP, can you help me?
Here is my code(I remove some unnecessary lines): page1
<form method="get" action="home.php">
<div class="form-group">
<input id="codehe" class="form-control" type="codehe" name="codehe" placeholder="Class Code">
</div>
<div class="form-group">
<input class="form-control button" type="submit">
</div>
</form>
page2 (this is how i get the value from page1)
<?php
$codehe = $_POST['codehe'];
echo $codehe;
?>
The other GET (form) on page2
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
</form>
<?php
if(isset($_GET['state'])) {
$str = $_GET["state"];
$sql = "SELECT * FROM classvideo WHERE subjects = '$str' AND linkcode = ''";
?>
The traditional way to do things like this (without client-side code) is to put a hidden input in your form on page 2:
<form method="GET">
<select id="state" multiple name="state" onchange='if(this.value != 0) { this.form.submit(); }'>
<option value="ALL">ALL</option>
<option value="<?php echo $row['subjects'];?>"><?php echo $row['subjects'];?></option>
</select>
<!-- Add this line -->
<input type="hidden" name="codehe" value="<?php echo $_GET['codehe'] %>">
</form>
I am making a online application used for storing user data and order information. I have successfully managed to create a system to which you first add customers and then add orders to specific customers.
I now want to create a function that would let the administrator of the page edit a certain line in the table.
I managed to figure out how to manually change it through a MySql code but I cannot figure out how to change it through a form? Right now I have this, but I do not think this is the correct way to do it... It works, but is very limited. For example, I am not sure what to do, if admin selects date?
PHP AND HTML
<?php
$id = $_GET['id'];
?>
<form action="update.php" method="POST" >
<input type="text" name="id" style="display:none;" value="<?php echo $id; ?> placeholder="Hidden id">
</select>
<select name="change"style="width:32.9%;" required>
<option selected hidden value="">Select a entry you want to edit</option>
<option value="narocnik">Customer</option>
<option value="naslov">Adress</option>
<option value="kraj">City</option>
<option value="enaslov">E-mail</option>
<option value="telefon">Number</option>
<option value="datumzacetka">Start date</option>
<option value="datumkonca">due date</option>
<option value="material">Material</option>
</select>
<input type="text" name="new" style="width:32.5%;" placeholder="New entry">
<input type="submit" value="PoĊĦlji">
</form>
AND THE MYSQL PART
<?php
include 'baza.php';
$change = $_POST['change'];
$new = $_POST['new'];
$id = $_GET['id'];
$id = $_POST['id'];
$sql = "UPDATE projekti SET $change='$new' where id='$id'";
if ($conn->query($sql) === TRUE) {
header("Location: projekti.php?id=$id");
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
As I said, the function works, but it is far from perfect, so any suggestions would be very welcome :) The most important thing would be the ability to have all the fields displayed but only update the newly inputted ones.
So this is my sample code, it takes an input from a select tag and a textbox. The problem is,most examples I see on google only use a single select tag within a form tag and posting the data to the php variable. I need to get both of these values to the query simultaneously. Should I use javascript instead to get the value of the select tag, or is there any way to do post both the data from the textbox and a select tag in a single form?
employee.php
<form name = "AddEmployee"method="post" action ="php/employeetabsubmit.php" >
<input type="text" name="surname" id = "surname" placeholder="Surname Here">
<select name="empStanding">
<option value="ADMIN">ADMIN</option>
<option value="EMPLOYEE">EMPLOYEE</option>
<option value="SUPERVISOR">SUPERVISOR</option>
</select>
<button class="add" type="submit" name="add">Add</button>
</form>
employeetabsubmit.php
<?php
$conn=mysqli_connect('localhost','root','','employee');
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
else{
echo '<script>console.log("Connected to DATABASE")</script>';
}
if (isset($_POST['add'])){
$surname_string = mysqli_real_escape_string($conn, $_POST['surname']);//sample input
$employee_standing = $_POST['empStanding'];
$sql="INSERT INTO employee_table (emp_surname, emp_standing) VALUES ('$surname_string','$employee_standing');
if (!mysqli_query($conn,$sql)) {
die('Error: ' . mysqli_error($conn));
}
echo "1 record added";
mysqli_close($conn);
}
?>
Put the button outside the select tag, and it should be working - Everything else looks correct
Add enctype="multipart/form-data" in form attribute.
<form name = "AddEmployee"method="post" action ="php/employeetabsubmit.php" enctype="multipart/form-data">
<input type="text" name="surname" id = "surname" placeholder="Surname Here">
<select name="empStanding">
<option value="ADMIN">ADMIN</option>
<option value="EMPLOYEE">EMPLOYEE</option>
<option value="SUPERVISOR">SUPERVISOR</option>
</select>
<button class="add" type="submit" name="add">Add</button>
</form>
i am trying to create an PDF using Dompdf, in my program i have model query with a dynamic parameter based on the input in the form, but when i click on my button to send the value from the select button to the controller, the data not been send properly, any idea why? below are my code
<form class="form" method="get" action="<?=site_url()?>/laporan/pdfdownload" id="myID" name="myName">
<select id="sel1" class="form-control">
<option disabled selected="selected">Pilih</option>
<?php foreach ($kerja as $rows){?>
<option value="<?php echo $rows->id_project?>"><?php echo $rows->id_project.' - '.$rows->nama_project ?></option>
<?php }?>
</select>
<select id="sel2" class="form-control">
<option disabled selected="selected">Pilih</option>
<?php foreach ($item as $rows){?>
<option value="<?php echo $rows->id_project?>"><?php echo $rows->id_project.' - '.$rows->nama_project ?></option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px;margin-left: 28px;width: auto" name="filter_button" type="submit" class="form btn btn-danger"><i class="fa fa-search"></i> Search</button>
</form>
and here is my controller
public function pdfdownload(){
//If i click submit then all of the post didnt get sended
$one = $this->input->post('sel1');//no value at all, anyone know why?
$two = $this->input->post('sel2');//no value at all, anyone know why?
$data['real'] = $this->report_m($one,$two)->row();
htmlcontent = $this->load->view('laporan/download/laporan3.php',$data,true);
include(APPPATH."third_party/dompdf/autoload.inc.php");
// require_once APPPATH . 'third_party/dompdf/autoload.inc.php';
$dompdf = new Dompdf\Dompdf();
$dompdf->load_html($htmlcontent);
$dompdf->set_paper("f4");
$dompdf->render();
$dompdf->stream("cobadlu.pdf",array("Attachment" => false));
exit(0);
}
enter code here
In your HTML form, you need to use name attribute, and not only ID :
<select id="sel2" name="sel2" class="form-control">
First of all there is a mistake in your form:
<form class="form" method="get" action="<?=site_url()?>/laporan/pdfdownload" id="myID" name="myName">
You given the method='get' here, please change it to 'post'.
Second you are not include the 'name' attribute to the input type so after submit no data will send. So add the name attribute like that:
<select id="sel1" name="sel1" class="form-control">
<select id="sel2" name="sel2" class="form-control">
I want to create a html form with 2 nested select box,
And I want to do like that ,
1 selectbox , and user can select
Modern Or Ancient . I did it ;
If User Select ancient , new options are :
Age : (textfield)
Determined By: (text field)
Age By c-14 testing
And,
If C14 testing is performed
Name of the body performing the test
Date of testing
Here's my HTML so far:
<select name="storedinage" id="storedinage" onchange="showfield7(this.options[this.selectedIndex].value)">
<option>Please Select</option>
<option value="Modern" >Modern</option>
<option value="Ancient" >Ancient </option>
and my JavaScript is here:
<script type="text/javascript">
function showfield7(name){
if(name=='Ancient')document.getElementById('div7').innerHTML='Other: <input type="text" name="storedinage" />';
else document.getElementById('div7').innerHTML='';
}
</script>
Check jsfiddle here
I think this is what you wanted. Let me know if otherwise.
I am not sure how your html looks like so I made a bit of my own, please copy what is useful. Don't know either if you want labels on inputs or placeholders, you will have to do those small adjustments.
html
<form action="#" method="post">
<select name="storedinage" id="storedinage" onchange="showfield(this.options[this.selectedIndex].value)">
<option>Please Select</option>
<option value="Modern">Modern</option>
<option value="Ancient">Ancient</option>
</select>
<div id="div7" style="display:none;">
Age:<input type="text" name="storedinage" />
Determined By:<input type="text" name="DeterminedBy" />
Age By c-14 testing?<input type="checkbox" onchange="C14(this)" name="AgeByc-14testing" />
</div>
<div id="C14" style="display:none;">
<input type="text" name="NameBody" placeholder="Name of the body performing the test" />
<input type="text" name="TestingDate" placeholder="Date of testing" />
</div>
</form>
js
function showfield(name) {
console.log('!');
if (name == 'Ancient') {
console.log('passed');
document.getElementById('div7').style.display = 'inline';
} else {
document.getElementById('div7').style.display = 'none';
}
var checkbox = document.getElementById('C14');
if (checkbox.checked == true) {
console.log('true')
}
}
function C14(e) {
if (e.checked == true) {
document.getElementById('C14').style.display = 'inline';
}
}