I want to make an image which is clickable and in case when its clicked I use $_POST to get it's value and use it. It looks like this:
if (isset($_POST['checked'])){
$q = mysql_query("UPDATE table SET checked = 2 WHERE tr_id = ". $_POST['checked']);
}
<form action="" method="POST">
<input type="image" src="images/checked.png" name="checked" value="<?php echo $info[0]; ?" />
</form>
Well in Chrome it works, but then I realised that in IE and Mozilla it doesn't work, can anyone suggest me same thing but so it works on all browsers?
Main thing is that this images value field get it's value from database with mysql_fetch_row and I must use this value in update query.
It would be nice if you close the double quotes and the input tag:
<input type="image" src="images/checked.png" name="checked" value="<?php echo $info[0]; ?>">
You are missing double quotes " and > there at the end :D
value="<?php echo $info[0];">
It should work even in other browsers, but if you want rich HTML in your buttons, just stick with <button>:
<button>
<img src="someImg.jpg" alt="">
</button>
This will submit your <form> as well.
Related
This is my second code but the problem is I have 3 queries. So it only returns the last product_id when i Click update it always return product_id=3, but i want update the product_id=2
<form action="update_qty.php" method="POST">
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
<?php } ?>
</form>
Your problem is that the PHP is server side and you need something client side to read the value of the text box. You would need a page refresh to pass the text field value to the server so it could write it to the url in the anchor tag. Which is what the form submit would do, but as it would have submitted the value already the anchor tag would be pointless
To do it without a page refresh use Javascript. It would be easy to do with jQuery. You could add an event that writes whatever is entered in the text box the the anchor tags href as it is typed.
I'll do something more like this.
One form per product.In your case when you submit the form the qty value will always be the las found.
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<form action="update_qty.php" method="POST">
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
</form>
<?php } ?>
You can add more information like this
update
You can not get all values as like that because input name overwrite in every loop iteration.
For multiple values you can try in two ways like:
<?php
while($getorder = mysqli_fetch_array($order)){
$newArr[] = $getorder['price']."~".$getorder['product_id']."~". $ getorder['qty'];
} //while end
?>
<input type="hidden" name="allinputs" value="<?=$newArr?>">
Input field outside the loop.
In php explode array value with ~ and get the all values.
Other solution is that
Your input field name must be change like:
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price_<?=$getorder['product_id']?>">
<?php } ?>
Change field name in every iteration.
In current scenario either you need three different buttons or the best solution to use AJAX request .
update
On update_qty.php u can use like this
<?php echo $_GET['product_id'];?>
I am trying to pass a value that I have received from my database to another php page to use within another SQL statement.
I have tried using sessions and also passing using the $_POST method on the other page but have had no luck.
Here is a snippet of my code looping through to display all records:
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src='.'"'.$row['image'].'"'.'><br/>
<form method="post" action="edit-record.php">
<input type="text" name="imgID" value='.'"'.$row['id'].'"'.'>
<input type="submit" value="Edit" id="edit_btn" class="admin_btn"></form>
</td>';
}
The value that I need is the ID for each specific image - $row['id'].
When the user clicks the EDIT button, they should be redirected to another page which displays only the specific record. This is why I need the ID received passed to the next page to insert into a query statement.
I hope this made sense and any help will be greatly appreciated.
UPDATE: Thanks for all of your help. I solved the problem by playing around with a few of your suggestions to pass the id via GET in the action of the form.
<form method="post" action="edit-record.php?id='. $row['id'].'">
No idea why that hadn't occurred to me! Thanks again.
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src="'.$row['image'].'"><br/>
<form method="post" action="edit-record.php">
<input type="text" name="imgID" value="'.$row['id'].'">
<input type="submit" value="Edit" id="edit_btn" class="admin_btn">
</form>
</td>';
}
in edit-record.php...
<?php
echo $_POST['imgID'];
?>
There is no reason your code technically wouldn't work but instead you could just eliminate the form and use a simple link...
while($row = mysqli_fetch_array($sql)){
echo '<td>
<img src="'.$row['image'].'"><br/>
edit
</td>';
}
and in edit-record.php...
<?php
echo $_GET['id'];
?>
4 Ways to do this...
1) Use a cookie
2) Use a session (which by default uses a cookie but in a different way)
3) Use cURL
4) add it to the GET parameters... ie. somepage.com/page.php?id=1
Strange concatenation
<input type="text" name="imgID" value="'.$row['id'].'">
Sure you select id on the mysql query???
If you make
echo $_POST['imgID'];
what is the result???
You can pass the id via get in the action form:
<form method="post" action="edit-record.php?id='. $row['id'].' ">
On the other page you recive the form in $_POST and the id in $_GET['id']
~Aha, I think the problem is your quotes. Single quotes don't allow variables to be interpreted.~
Nevermind, thats not your problem, but I already wrote it out, so I'll leave it. Look how much cleaner those quotes are :)
Switch up your quotes like so:
echo "<td>
<img src='{$row['image']}'><br/>
<form method='post' action='edit-record.php'>
<input type='text' name='imgID' value='{$row['id']'}'>
<input type='submit' value='Edit' id='edit_btn' class='admin_btn'></form>
</td>";
Need curly braces around array element (e.g {$row['id']})
I want to make a picture send a specific value tied to that image when clicked to a php file that will make a mySQL query.
For example let say I have a HTML page of animal pictures that when a specific animal image is clicked, say an image of a Cat, it would sent that as a value to be assigned to a variable that is then put into a MySQL query that will pull out different breeds of cats from an animal database containing cats,dogs, horses, ect.
So in the HTML page, animals.html, I am creating the images with this code:
//animals.html
<form action="animalQuery.php" method="get"> <input type="image" src="Cat.jpg" name="cats" width="350" height="225">
<form action="animalQuery.php" method="get"> <input type="image" src="Dog.jpg" name="dogs" width="350" height="225">
When the Cat.jpg image is clicked I want to send the value “cats” to be assigned to $animal variable so that it basically makes the $stmt below equate to “select * from Animals where breeds = ‘cats’;" which is a working Query.
//animalQuery.php
$animal=$_GET["cats"];
$stmt="select * from Animals where breeds = $animal;";
It works if I hard code the $stmt with value of “cats” or “dogs” in place of the variable $animal, so I know the Query works. I am just having trouble figuring out how to have the image when clicked send the specific value I want to that variable $animal. Once I figure that out I should be able to figure out how to have the single variable $animal get set to a certain value when one of the many images is clicked so that one variable works for all the images.
Why a full-blown form for something so simple?
<img src="..." />
<img src="..." />
In the form, give the buttons the same name, and catch the value in PHP. You can do that like this:
<form action="animalQuery.php" method="get">
<input type="image" src="Cat.jpg" name="animal" value="Cat" width="350" height="225">
<input type="image" src="Dog.jpg" name="animal" value="Dog" width="350" height="225">
</form>
<?php
//Set the possible values
$animals = array( "Cat", "Dog");
//Check if the form is submitted and set the value if the value exists
$animal = isset( $_GET['animal']) &&
in_array( $_GET['animal'], $animals)? $_GET['animal'] : "Not chosen";
An alternate way to approach your problem might be to replace your inputs with links. For example:
<img src="Cat.jpg" width="350" height="225" />
<img src="Dog.jpg" width="350" height="225" />
You could then update animalQuery.php to work like this:
$animal=$_GET['animal'];
You can see an example of this on this page:
https://stackoverflow.com/tags
You'll see that each tag is a link, not an input field:
c#
...
java
...
php
Finally, you should look into the MySQLi or PDO library for writing your SQL. If someone were to visit this page: animalQuery.php?animal=cats UNION SELECT * FROM information_schema.tables, then they might be able to hack your site.
I had a similar problem to this a while back, and it came about because some browsers send image coordinates as the values as opposed to the actual value you specify. The easiest way to solve that particular problem (if you have to use image inputs) would be to include hidden values.
<form action="animalQuery.php" method="GET">
<input type="hidden" name="animal" value="cat" />
<input type="image" src="Cat.jpg" />
</form>
Then retrieve the value in your animalQuery.php file.
<?php
$animal = $_GET['animal'];
$stmt = "select * from Animals where breeds = '$animal';";
?>
I'm trying to get a website working. What I have are basically two images displayed (random, taken out of a mySQL database). What I need to do is (when the user clicks one of the images) the following:
Update the page, passing the info about the selected image (submit form);
Add one piece of data to the database (upvote the image)
I need to use $_POST to pass an array of values to the next page. So I thought:
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image.png"
value ="dat1[\"data1\",\"data2\",\"data3\"]">
<!-- If value must be a single string, I'll use hidden inputs-->
</form>
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image2.png"
value ="dat2[\"data1\",\"data2\",\"data3\"]">
</form>
Then I can upvote the selected image on the mySQL database with a little php upvote() function that updates the record. The upvoting process is done when the new page is loaded. From this, I have a couple questions:
I'm guessing the images will act as buttons, right? (They are supposed to submit the form, hence refreshing the page). If not, how can I achieve this? I'm unable to do it with a link (since I can't add the values to it). Maybe a javascript function? But I don't know how to submit the form that way either...
Once the page is reloaded, does it mean that only the data from one form has been submited, so I can retrieve the data by simply calling the PHP variable $_POST['img'] and get an array back?
EDIT: I now managed to get everything working, slightly similar to what I proposed initially. Thanks for the AJAX suggestion though, since it was what helped me solve it (looked up AJAX tutorials, found solution).
Here's my solution:
<?php
echo "<form name=\"input\" action=\"F2F.php\" method=\"POST\">";
echo "<input type=\"hidden\" name =\"table\" value=\"".$table1."\">";
echo "<input type=\"image\" name=\"nom\" src=\"".$IMG_Route1."\" value =\"".$Nom_base1."\" border=\"0\">";
echo "</form>";
?>
(where the image goes)
and then, on the header:
<?php
if ($_POST['nom']||$_POST['nom_x']){
if (!$_POST['nom']){
echo 'Could not retrieve name. $_POST[\'nom_x\'] = '.$_POST['nom_x']. mysql_error();
exit;
}
if (!$_POST['table']){
echo 'Could not retrieve table. $_POST[\'table\'] = '.$_POST['table']. mysql_error();
exit;
}
upvote($_POST['table'],$_POST['nom']);
}
?>
You can use one form and a set of radio buttons to simplify things a bit. Clicking on the label will toggle the radio button. You can use commas to separate multiple values for each checkbox, which you can then abstract later on (see below)
<form name="input" action="the_page.php" method="POST">
<ul>
<li>
<label>
<img src="whatever.jpg" />
<input type="radio" name="selectedImage" id="img1" value="12,16,19" />
</label>
</li>
<li>
<label>
<img src="whatever2.jpg" />
<input type="radio" name="selectedImage" id="img2" value="12,16,19" />
</label>
</li>
</ul>
</form>
You can detect when the radio button is selected by adding a listener for the change event, then submit the form.
$('input[name="selectedImage"]').change(function() {
$('form[name="input"]').submit();
});
To abstract the multiple values, you can then explode the form result with PHP, which will return an array of the values.
$selectedImageValues = array();
$selectedImageValues = explode(",", $_POST['selectedImage']);
From there you can pull the different values out and save the data to the database.
I need to get a value from a text box, but I'm not using a form, therefore I can't use $_POST or $_GET. Is there any method to get that value from the text box? Below is the code that I'm using
<input name="txtQty" type="text" id="txtQty" size="5" value="<?php echo $qty; ?>" onKeyUp="checkNumber(this);"> //here I'm the displaying the quantity, then the user can change it, and I need to get that value to pass it throught this link:
<input name="btnEdit" type="button" value="Edit" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=update&cid=$shoppingCartId&qty=$qty //here I need that new value from the textbox"; ?>';"
Thanks for your help
If you're doing this without a page refresh you need javascript. If you do have a page refresh, you need a form.
Without form or javascript controlled request - no you can't
Ajax must be used to Pass client data to sever if not wish to refresh the page. Check below code, which was written with simple changes.
<input name="txtQty" type="text" id="txtQty" size="5" value="<?php echo $qty; ?>">
<input name="btnEdit" type="button" value="Edit" id="btnEdit" onclick="redirectUser()" />
<script>
function redirectUser(){
var qty = document.getElementById('txtQty').value;
location.replace('<?php echo $_SERVER['PHP_SELF']; ?>action=update&cid=<?php echo $shoppingCartId; ?>&qty='+qty);
}
</script>
PHP only runs on the server, so the only way to get the values from your inputs would be to somehow pass them to the server.
You could do it in a number of ways, but the easiest would likely be to put your inputs into a form, and post the form to a PHP script.
You can use javascript:
document.getElementById("txtQty").value
Why are you saving text box box variable in php?
If you are passing value to text box in php, then you can echo php value over there.
<input name="btnEdit" type="button" value="Edit" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=update&cid=$shoppingCartId&qty="+<?php echo $qty; ?>';"
If not then you can use directly form item as value over there.
<input name="btnEdit" type="button" value="Edit" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=update&cid=$shoppingCartId&qty="+document.getElementById("txtQty").value';"