Get the value of the selected image - php

Currently I'm working on a mobile version of an article publishing application. I want to let the user search for their image like you are searching through images on Google Images(Fill in the keywords, click an image, swipe back and forth between images)
The user can select their keyword(s) and after that an selection of images will be shown(like a gallery/slideshow). When they swipe through these images and stop at an particular image I would like to know that current ID value (that is coming from the database). I am not getting any information if I use the 'value' field in an <img> tag or an <input type='image' /> tag furthermore I can't think of any other solution at the moment.
This sounds a bit abstract, although I still hope that someone can give me a suggestion
EDIT:
For example, when I'm only trying it with 1 image I have these 2 possibilities:
$sql = "SELECT *
FROM picture
WHERE picture_id = :picture";
$con->query($sql);
$con->bind(":picture", $picture_id);
$con->execute();
$record = $con->getRow();
echo "<input type='image' name='image' value=".$record["picture_id"]." src=\"http://www.mysite.com/img/".$record['picture_directory'].$record['picture_name']."\" />";
or when I use:
echo "<img name=\"image\" src=\"http://www.mysite.com/img/".$record['picture_directory'].$record['picture_name']."\" alt=\"Smiley face\" value=". $record["picture_id"]. " height='42' width='42'>";
When I submit the form to the next page, I am not getting the value of the 'name' attribute

You could use a data attribute:
<img src="example.png" data-picture_id="'.$record['picture_id'].'" />
and fill a hidden input field as soon as the user clicks the image:
<input type="hidden" name="selected_image" value="" />
//Bind click to images that have the data-picture_id attribute:
$("img[data-picture_id]").click(function(e){
//Set the value of the hidden input field
$("input[name='selected_image']").val($(this).data('picture_id'));
});
this way, you can use $_POST['selected_image'] to get the selected image's ID.

What you could do is use html5 data attributes.
In your php code generate your images similar to this:
foreach($images as $key => $img){
echo '<img src="'.$img['path'].'" data-image-id="'.$img['image_id'].'">'
}
or even use the elements default id attribute
foreach($images as $key => $img){
echo '<img src="'.$img['path'].'" id="img'.$img['image_id'].'">'
}
Eitherway what you should do next is when the user stops sliding / swiping check the images position offset relative to the container to get the id of the current image. Or depending on your slider keep track of the current image index being shown.
These might come handy:
jquery .offset() http://api.jquery.com/offset/
jquery .position() http://api.jquery.com/position/

Why not use a hidden input and a regular image tag? Just make sure it's inside the form.
<input type="hidden" name="picture_id" value="<?php echo $record["picture_id"] ?>">
A few things, just adding a value attribute to an HTML element won't make it behave like an input. Also, using a value on an input type image just doesn't work. You will have the X and Y of where the image would have been clicked associated with the input's name (I believe).

Related

display a table when a button is pressed [duplicate]

I'm having trouble adding the value of type="button" form elements to a mySql database, and I'm wondering if I'm missing something.
Edit - It doesn't look like the information for that element is being passed from the html to the php because it's not echoing a value. My only problem is with this one element and the rest of the form is being submitted properly.
I'm using this for an online quiz which builds a user profile based upon images they've selected, and am setting the images as background images for the button elements, and I'm trying to do this in straight html (as opposed to using javascript together with radio buttons or check boxes).
<input type="button" name="quiz_start" value="jeans" style="background: url(files/start1.jpg) no-repeat; width:54px;height:140px; cursor:pointer; border:none; color: transparent; font-size : 0">
I've simplified the php code for purposes of asking the question (including specifying the user id and limiting it to only one field). I've also included the full code below.
<?php
//Start session & connect to database
$user_id = 3;
$qry = "INSERT INTO style(user_id, quiz_start) VALUES('$user_id','$_POST[quiz_start]')";
$result = #mysql_query($qry);
header("location: page2.html");
exit();
?>
The full query is:
$fieldlist=$vallist='';
foreach ($_POST as $key => $value) {
$fieldlist.=$key.',';
$vallist.='\''.($value).'\',';
}
$fieldlist=substr($fieldlist, 0, -1);
$vallist=substr($vallist, 0, -1);
$fieldlist.=', user_id';
$vallist.=','.$user_id;
$setlist='';
foreach ($_POST as $key=>$value){
$setlist.=$key .'=\''.$value.'\',';
}
$setlist=substr($setlist, 0, -1);
$result = mysql_query('UPDATE style SET '.$setlist.' WHERE user_id='.$user_id);
if (mysql_affected_rows()==0) {
$result = mysql_query('INSERT INTO style ('.$fieldlist.') VALUES ('.$vallist.')');
}
header("location: page2.html");
exit();
?>
Seeing that you are unable to echo $_POST['quiz_start'] that means your value is not actually set. This is because when you use a class button as in <input type='button'> your form is not actually submitted like <input type='submit'>
One solution would be to change your button to an actual submit and format that... or you need to call a javascript function with an onClick from your button as in:
<input type="button" onClick="myfunction()">
For reference to what I am talking about look at this post.
If as you say the rest of the form values are submitting fine but just the button value is not working you have a few different possible solutions depending on your preference.
Use a select field or checkbox for people to select a type in which you can pass your data.
Submit your form in javascript with <input type="button" onClick="myfunction()"> then running your update query in javascript.
Finally if you still want to run your query in PHP you can run a javascript function to make an AJAX call to return JSON information in which you can define a php variable after the page has loaded in which you can then plug into your update query.
Try this:
<?php
//Start session & connect to database
$user_id = 3;
$qry = "INSERT INTO style(user_id, quiz_start) VALUES('".$user_id."','".$_POST['quiz_start']."')";
$result = #mysql_query($qry);
header("location: page2.html");
exit();
?>
Since it seems that input type="button" can't capture the user's selection, I wanted to share a really simple way I figured out for doing this with radio buttons or check boxes using only html.
All you need to do is set the input element to style="display:none", and surround both the image and input element with a label tag so that users can click anywhere on the image to select the element :-)
<label for="quiz_start">
<img src="files/start1.jpg" />
<input style="display:none" type="radio" id="quiz_start" name="quiz_start" value="jeans">
</label>

How to populate input type file value from database in PHP? [duplicate]

This question already has an answer here:
Echo the value inside html input type=file
(1 answer)
Closed 8 days ago.
I am writing the code for editing a form that contains an input file field. I am getting all the values pulled from database for different field types but the file type input does not show its value.
I have a code that looks like this:
<input class="picturebox" id="logo" name="userfile" value="<?php echo $discount_details->picture_name;?>" />
But actually in rendered view value attribute is null for userfile field.
How do I load the value of input type when someone is editing the form and does not want to alter the picture entered earlier by him upon edit.
you can give the value attribute to input file type
if you want to show the file content while updating form you can show it in separate tag
like:
<input type="file" /> <span><?php echo $row[column_name]?></span>
here you should consider one thing
if the use is selected new file to upload you can update the column else the use not selected any thing just updated other content without file you should update the column name with old file name.
$file = $_FILES['file']['name'];
if($file!="") {
move_uploaded_file($_FILES['file']['tmp_name'],'/image/'.$file);
} else {
$file = $oldfile;
}
You can just make value field empty and show your old image at just up of that input field(or below).then check after submitting form, if $_POST['userfile'] is empty don't update table.picture_name.
The simple trick is that ; give an id to the tag
<input type="file" name="file" /> <span name="old" value="<?=$row[column_name]?>"><?php echo $row[column_name]?></span>
Then in PHP make it like this:
$oldfile = $_POST['old'];
$file = $_FILES['file']['name'];
if($file!="") {
move_uploaded_file($_FILES['file']['tmp_name'],'/image/'.$file);
} else {
$file = $oldfile;
}
you can write this way
Step 1: Fetch your image in a variable. like this $pimg = $result['pimage'];
Step 2: write html code for add file. <input type="file" name=""image">
Step3: in PHP fetch the file if image upload by user. like this $pimage = $_FILES['images']['name'];
Step 4: now check if the user uploaded the file or not.
if(empty(file name)){
if yes then update image it.
}else{
if no then priviously uploaded image use it.
}

Value from input type="button" isn't being added to database

I'm having trouble adding the value of type="button" form elements to a mySql database, and I'm wondering if I'm missing something.
Edit - It doesn't look like the information for that element is being passed from the html to the php because it's not echoing a value. My only problem is with this one element and the rest of the form is being submitted properly.
I'm using this for an online quiz which builds a user profile based upon images they've selected, and am setting the images as background images for the button elements, and I'm trying to do this in straight html (as opposed to using javascript together with radio buttons or check boxes).
<input type="button" name="quiz_start" value="jeans" style="background: url(files/start1.jpg) no-repeat; width:54px;height:140px; cursor:pointer; border:none; color: transparent; font-size : 0">
I've simplified the php code for purposes of asking the question (including specifying the user id and limiting it to only one field). I've also included the full code below.
<?php
//Start session & connect to database
$user_id = 3;
$qry = "INSERT INTO style(user_id, quiz_start) VALUES('$user_id','$_POST[quiz_start]')";
$result = #mysql_query($qry);
header("location: page2.html");
exit();
?>
The full query is:
$fieldlist=$vallist='';
foreach ($_POST as $key => $value) {
$fieldlist.=$key.',';
$vallist.='\''.($value).'\',';
}
$fieldlist=substr($fieldlist, 0, -1);
$vallist=substr($vallist, 0, -1);
$fieldlist.=', user_id';
$vallist.=','.$user_id;
$setlist='';
foreach ($_POST as $key=>$value){
$setlist.=$key .'=\''.$value.'\',';
}
$setlist=substr($setlist, 0, -1);
$result = mysql_query('UPDATE style SET '.$setlist.' WHERE user_id='.$user_id);
if (mysql_affected_rows()==0) {
$result = mysql_query('INSERT INTO style ('.$fieldlist.') VALUES ('.$vallist.')');
}
header("location: page2.html");
exit();
?>
Seeing that you are unable to echo $_POST['quiz_start'] that means your value is not actually set. This is because when you use a class button as in <input type='button'> your form is not actually submitted like <input type='submit'>
One solution would be to change your button to an actual submit and format that... or you need to call a javascript function with an onClick from your button as in:
<input type="button" onClick="myfunction()">
For reference to what I am talking about look at this post.
If as you say the rest of the form values are submitting fine but just the button value is not working you have a few different possible solutions depending on your preference.
Use a select field or checkbox for people to select a type in which you can pass your data.
Submit your form in javascript with <input type="button" onClick="myfunction()"> then running your update query in javascript.
Finally if you still want to run your query in PHP you can run a javascript function to make an AJAX call to return JSON information in which you can define a php variable after the page has loaded in which you can then plug into your update query.
Try this:
<?php
//Start session & connect to database
$user_id = 3;
$qry = "INSERT INTO style(user_id, quiz_start) VALUES('".$user_id."','".$_POST['quiz_start']."')";
$result = #mysql_query($qry);
header("location: page2.html");
exit();
?>
Since it seems that input type="button" can't capture the user's selection, I wanted to share a really simple way I figured out for doing this with radio buttons or check boxes using only html.
All you need to do is set the input element to style="display:none", and surround both the image and input element with a label tag so that users can click anywhere on the image to select the element :-)
<label for="quiz_start">
<img src="files/start1.jpg" />
<input style="display:none" type="radio" id="quiz_start" name="quiz_start" value="jeans">
</label>

Redundant way of getting multiple $_POST records?

EDIT : I might just make it a more 'step-by-step' process i.e.
Seeing as it feels more user-friendly & it's only for <5 images. I have read & appreciate all of the help that I have received so far.
I'm fairly new to PHP & I'm building a basic PHP image editor which may allow multiple image data auditing which is then inserted into a MySQL database. A limit of 5 images may be uploaded at a time for this system (implying that there will only be a low amount of records to play with), but I'll get to that further down.
GUI:
Some of the data columns used:
image id
caption
description
published
imageposition
delete button
All images are echo'd in a while loop by a previous 'SELECT *' result set. Each image will have the 'image id' echo'd inside each input name, so output will be for example:
caption-2, description-2, published-2, imageposition-2, caption-3, description-3
Also, the data values from the previous resultset are echo'd into the input value, allowing the client to edit the current data that exists in the database. There is only one submit button.
My question:
I want to be able to post all of the modified image(s) data to a processor.php form which will then allow me to insert it as an INSERT sql string into the MySQL images table. I need the PHP to be dynamic incase:
images are deleted (image id)
new images are uploaded (image id)
If there is an easier way of doing any of the above, I am welcome to new ideas/opinions. Sorry in advance for my poor understanding of PHP.
Once you get result from 'select *' statement you do foreach
foreach ($images as $image) {
?>
<img src="imagesrc">
<input type="text" name="title_<?php echo $image['image_id'] ; ?>" value="<?php echo $image['title']; ?>">
<input type="text" name="desc_<?php echo $image['image_id'] ; ?>" value="<?php echo $image['description']; ?>">
<input type="button" onclick="markDeleted('<?php echo $image['image_id'] ; ?>')">
<?php } ?>
.....
Once you receive the post value you can explode with "_" and you will get the Primary key and you can update the image table with image ID.
In the javascript function markDeleted you can set the primary key in some hidden field and sent to process.php.
Add it like comma separated 1,2,3 like this (Split by comma in action page)
function markDeleted (imageId)
{
document.getElementById('deleted_image_ids').value = imageId + ","
}
To delete the records easily :
<form action="processor.php" method="post">
<?php
foreach ($images as $img) { // assuming $images is the result set of your sql query
?>
<input type="text" name="name<?php $img['image_id'];?>" value="<?php $img['caption'];?>">
<input type="text" name="desc<?php $img['image_id'];?>" value="<?php $img['description'];?>">
...
<a href="http://yoursiteurl/deleteimg.php?id=<?php $img['image_id']?>">
<?php } ?>
</form>
In deleteimg.php :
<?php
$del = $_POST[id];
$query = $msqli->prepare("DELETE FROM imagetablename WHERE image_id=?");
$query->bind_param( 's', $del );
$query->;execute();
?>
Updating and adding new rows may involve javascript, and much more harder.

Enable/Disable a specific dynamically-added form-input

I'm creating an HTML form, which takes some of its options from a database (php + mysql)
In the php, I'm creating a checkbox input, with a select box next to it.
I named the checkbox houseAppsSelected[] and the select customCategories[], so I'll get the values as an array.
I append all the HTML into a var called $options, and I echo it later on.
while ($row=mysql_fetch_array($result)) {
$cat_id=$row["category_id"];
$cat_name=$row["category_name"];
$options.="<INPUT type=\"checkbox\" name=\"houseAppsSelected[]\" VALUE=\"$cat_id\">".$cat_name." ---> ";
$custom_sql="SELECT custom_cat_id, cat_name FROM custom_categories WHERE house_app='$cat_id'";
$custom_result=mysql_query($custom_sql);
$options.="<SELECT name=\"customCategories[]\">";
$options.="<OPTION value=\"0\"> Choose Category </option>";
while ($custom_row=mysql_fetch_array($custom_result)) {
$custom_id = $custom_row['custom_cat_id'];
$custom_name = $custom_row['cat_name'];
$options.="<OPTION value=\"$custom_id\">".$custom_name."</option>";
}
$options.="</SELECT> <br /> <br />";
}
I want to have the checkbox control whether the select box is enabled or disabled.
I found this article, which makes it look easy, but if all the select boxes have the same name, it will disable all of them.
Is there a way to have a specific checkbox disable/enable only a specific select box, if I build them dynamically with php? (they all have the same name).
You can use the nextSibling property to find the select.
function chkboxClick(chkbox) {
chkbox.nextSibling.nextSibling.disabled = !chkbox.checked;
}
Add the click handler like this:
<INPUT type="checkbox" onclick="chkboxClick(this)" ... />
Demo here: http://jsfiddle.net/gilly3/vAK7N/
You can give each tag a unique "id" value, which is independent of the "name". Then you can use
var elem = document.getElementById(something);
to access them by that unique value.
Exactly how your php code makes up the unique values sort-of depends on what you need, exactly. It can really be anything.

Categories