I am displaying images from a database, each image has a comment box that contains a form and a submit button; the submit button has the name of the image that is displayed. Ex. if the image name is flowers.jpg, the name for the submit button is set to flowers.jpg. (I did string replace though, so in my code it would be set to flowersjpg) The names are added to the submit button with no problem at all. Because I have several images, I wanted to pass $row['image'] (image name) into $_POST[ ] parameters for an isset() function but it is not working. - All the code is in one document
$result = mysqli_query($conn, "SELECT * FROM uploads ORDER BY timestamp DESC");
while($row = mysqli_fetch_array($result))
{
// Prints all the photos with a caption
echo "<div class='wrapper' name=".$row['image'].">";
echo "<div class='caption'>";
echo "<h3>".$row['title']."</h3>";
echo "<p class='description'>".$row['description']."</p>";
echo "</div>";
echo "<img src='images/uploads/".$row['image']."'/>";
// name for submit button, the $row['image'] contains the entire image name so certain characters have to be removed
**$name = str_replace(array(".", "-", ":", "'", "/", " "), "", $row['image']);**
// comment box - just a form with a submit
echo "<div class='commentBox'>
<form action='test.php' method='POST'>
<textarea placeholder='Say something nice!' name='comment'></textarea>
// CREATES NAME FOR BUTTON. FOR EACH IMAGE, THE BUTTON HAS THE IMG NAME
**<button type='submit' name='".$name."' value='submit'> post </button>**
</form>
</div>";
echo "</div>";
}
if(isset($_POST["'".$name."'"]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
When I put the name as a plain string in the $_POST parameters, it works fine but when I put the variable in, it does not work. The $name variable is a string.
This works but I'm sure any string used will work as long as the button name and string in $_POST are the same.
echo "<button type='submit' name='flowerspng' value='submit'> post </button>";
if(isset($_POST[flowerspng]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
This code on the other hand, does not. I know variables can be passed inside $_POST parameters, so I don't understand what's wrong with my code. Is my syntax incorrect? Clearly the variable is not being set otherwise the alert box would pop up.
echo "<button type='submit' name='".$name."' value='submit'> post </button>";
if(isset($_POST["'".$name."'"]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
I have tried this too but I don't think it worked because there are no quotes around $name.
echo "<button type='submit' name=$name value='submit'> post </button>"
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
This code works:
echo "<button type='submit' name='$name' value='submit'> post </button>"
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("isset"); </script>';
}
I moved the if statement for the isset() function into the end of the while loop, it works for all images now.
while($row = mysqli_fetch_array($result))
{
echo "<div class='wrapper' name=".$row['image'].">";
echo "<div class='caption'>";
echo "<h3>".$row['title']."</h3>";
echo "<p class='description'>".$row['description']."</p>";
echo "</div>";
echo "<img src='images/uploads/".$row['image']."'/>";
$name = str_replace(array(".", "-", ":", "'", "/", " "), "", $row['image']);
echo "<div class='commentBox'>
<form action='test.php' style='text-align: right; background-color: rgb(34,34,34); width: 100%; height: 120%; margin: 0;' method='POST'>
<textarea style='height: 90%; width: 79%; resize: none; border-radius: 3px; margin: 0; margin-top: 5px;' placeholder='Say something nice!' name='comment'></textarea>
<button type='submit' name='$name' value='submit' style='height: 100%; width: 20%; float: right;'> post </button>
</form>
<div class='seeMore'> see more </div>
</div>";
echo "</br><div class='comments' style='display: none;'> comment display </div>";
echo "</div>";
**
// testing the if statement for each image - works perfectly now
if(isset($_POST[$name]))
{
echo '<script type="text/javascript"> alert("'.$name.'"); </script>';
mysqli_query($conn, "UPDATE uploads SET title='test' WHERE image='".$row['image']."'");
header("Refresh:0");
}
**
}
Related
I am new on implementing Ajax on PHP so I need a big help! anyways my problem is I have a 5 column names on my table_menu
table_menu:
menu_id
menu_foodname
menu_price
menu_quantity
menu_image
and inside those column names it has 6 datas.
And I will gonna fetch those datas on my webpage(users.php)
<?php
// users.php
// fetching data
include 'config/initialize.php';
$sql = "SELECT * FROM table_menu";
$res = mysqli_query($con, $sql);
if($res)
{
echo "<table><tr>";
while($row = mysqli_fetch_array($res))
{
echo "<td>";
echo "<div class='horizontalAlign'>";
echo "<form class='form-item' method='post'>";
echo "<img class='img-circle' src='menuImage/".$row['menu_image']."' style='height: 150px; width: 200px;'>";
echo "<input type='hidden' name='menuimage' class='menuimage' value='".$row['menu_image']."'>";
echo "<input type='text' name='menufoodname' class='menufoodname' value='".$row['menu_foodname']."' style='border:none; margin-top: 10px; margin-left: 30px; font-size: 20px;' readonly>";
echo "<div style='font-size: 20px; margin-left: 40px;'>Price:</div> <input type='text' class='menuprice' name='menuprice' value='".$row['menu_price']."' style='border:none; margin-left: 100px; margin-top: -28px; position: absolute; font-size: 20px;' readonly>";
echo "<div style='font-size: 20px;'>Quantity:</div> <input type='number' class='menuquantity' name='menuquantity' style='margin-top: -26px; position: absolute; margin-left: 90px; text-align: center;' value='".$row['menu_quantity']."' min='0' max='100'>";
echo "<br>";
echo "<input type='button' value='Submit order' class='btnaddorder btn btn-lg btn-info' class='btn btn-info'>";
echo "</form>";
echo "</div>";
echo "</td>";
$i++;
if($i == 3)
{
echo "</tr><tr>";
}
} // end of while
echo "</tr></table>";
} // end of variable $i
?> <!-- // end of fetching data -->
and I want the page to be not refresh while the user orders... so I add this ajax script on the bottom of users.php
<script type="text/javascript">
$(document).ready(function() {
$(".btnaddorder").click(function() {
var menuimage = $(".menuimage").val();
var menufoodname = $(".menufoodname").val();
var menuprice = $(".menuprice").val();
var menuquantity = $(".menuquantity").val();
// Returns successful data submission message when the entered information is stored in database.
$.post("addorders.php", {
menuimage1: menuimage,
menufoodname1: menufoodname,
menuprice1: menuprice,
menuquantity1: menuquantity,
}, function(data) {
});
});
});
</script>
Now the problem is when I am clicking the first button on the fetched datas the first one only got inserted and if I am add the second item even the third until fifth item it only display one data on the database which is the first data, why?
heres my addorders.php
<?php
include 'config/initialize.php';
include 'credentials/credentialsForUsers.php';
$menuimage = $_POST['menuimage1'];
$menufoodname = $_POST['menufoodname1'];
$menuprice = $_POST['menuprice1'];
$menuquantity = $_POST['menuquantity1'];
$sql = "INSERT INTO table_orders VALUES('','$username','$email','$menuimage','$menufoodname','$menuprice','$menuquantity')";
$result = mysqli_query($con, $sql);
?>
thank you & sorry for my bad english. hope you understand my problem
To clarify which fields you want to submit you can do the following:
$(".btnaddorder").click(function() {
// here you find closest form tag to the pressed button
var form = $(this).closest('form');
// replace all you values with simple .serialize() function
// this function will use all non-disable fields from the form
$.post("addorders.php", form.serialize(), function(data) { });
});
On the serverside your $_POST will have the same keys as fields on your form.
I'm trying to embed some CSS into my PHP file. How can I properly add my CSS so that each called form will be rendered inside a black border? This is what I tried so far.
while ($ratings = mysql_fetch_array($q))
{
//This outputs the doctors's name
echo "Doctor's name:" . $ratings['doctor_name'] ."<br />";
// Retrieve the id of the doctor which was posted on
$id = $_POST['id'];
echo "<style> form { border-style: solid; border-color: #ffffff}";
//This outputs a textarea for the user to submit comments
echo "<b>Your Experience: </b>";
echo "<form method='post' action='review_doctors.php'>
<textarea name='body'></textarea>
<input type='submit' name='submit' value='Send'/>
<input type='hidden' name='id' value='$ratings[id]' />
</style>
</form>
";
echo "<br />";
}
As mentioned in the comments: "The style tag should only include styles".
?>
<style>
form {
border: 3px solid black;
}
</style>
<?php
while ($ratings = mysql_fetch_array($q)) {
echo "<form>...</form>"
}
If you would like to reuse styles you can put them in their own file and then include them on each page:
?>
<link rel="stylesheet" type="text/css" href="/path/to/mystyle.css">
<?php
// ...
I have stored images in phpmyadmin in a BLOB format. When I try to display them on my webpage, weird symbols and letters show up. The pictures are a mix of jog and png. Please help.
Here is the code that I currently have.
<?php
session_start();
include 'dbConn.php';
if (!isset($_SESSION['username'])){
header("Location: clientLogin.php");
}
$query = "SELECT * FROM ip_games ";
$stmt = $dbConn->query($query);
$result = $stmt->fetchAll();
?>
<style>
body {
background-color: grey;
}
table {
margin: 0 auto;
}
td {
padding-right: 30px;
}
</style>
<html>
<body>
<div>
<form action="welcome.php">
<input type="submit" value="Sign out" />
</form>
</div>
<h1>Happy Face Emoji Games</h1>
<form>
Name: <?php echo $_SESSION['name'] ?> <br />
</form>
<table>
<?
for ($i=0; $i<count($result); $i++)
{
echo "<tr>";
echo "<td>" . $result[$i]["picture"] . "<br />" . $result[$i]["name"]. " <br /> " .$result[$i]["console"] . " <br />" . "$" . $result[$i]["cost"] . "</td>";
//echo "<td>".$result[$i]["console"]."</td>";
//echo "<td>".$result[$i]["description"]."</td>";
//echo "<td>".$result[$i]["cost"]."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
To directly use the binary data as a an image source, you can use the data URI scheme, for example:
$uri = 'data:image/png;base64,'.base64_encode($row['binary-data']);
This URI can then be used directly as the image’s source:
background-image: url(<?php echo $uri; ?>)
<img src="<?php echo $uri; ?>">
not good practice, older browser issues, load issues and catching issues. cheer
You need to wrap your image in an <img> tag.
<img src="<?php echo $result[$i]["picture"]; ?>" />
Give that a try and let me know :).
Edit: I wrote that as if it was in the page, for your question you probably want:
echo "<td><img src='" . $result[$i]["picture"] . "' />" . "<br />"... etc.
Currently I have a script that displays the data which is editable and can update the database. I have tried to enter row counts and nothing seem to work. I really like the script to make 3 columns (10 rows per column), please help.
$sql = "SELECT id, pounds FROM price_list ORDER BY id";
$i = 0;
$result = mysql_query($sql);
echo "<form name='prices' method='post' action='updateA.php'>";
while($rows = mysql_fetch_array($result))
{
echo "<body bgColor='#5F5F6B'>";
echo "<table><table border=2 cellspacing=0 cellpadding=1>";
echo "<input type='hidden' name='id[$i]' value='{$rows['id']}' >";
echo "<td><font color='#FFFFFF'><font size='2'>DAYS {$rows['id']}: </font><font size='2'><font color='#000000'>PRICE:<input type='text' size='1' name='pounds[$i]' value='{$rows['pounds']}' ></tr>";
++$i;
}
echo "</table>";
echo "<input type='submit' value='Update Prices Band A' />";
echo "</form>";
?>
The above is the original code.
I don't really know what you're trying to do, but this code will generate a list of all the entries in the database with the ability to change them. Note that you'll have to remake your update_a.php file:
<style>
body {
background:#5F5F6B;
color:#fff;
}
</style>
<?php
$result = mysql_query("SELECT id, pounds FROM price_list ORDER BY id");
if (!$sql || mysql_num_rows($result)==0)
echo "Price list is empty";
else {
echo '<form name="prices" method="GET" action="update_a.php">'; // Change your filename!
$i = 0;
while ($rows = mysql_fetch_array($result)) {
echo 'Day '.$rows['id'].' costs ';
echo '<input type="text" name="'.$rows['id'].'" value="'.$rows['pounds'].'"/> pounds'
echo '<br/>'
$i++;
}
echo '<input type="submit" value="Update Prices Band A"/>';
echo "</form>";
}
?>
First of all many thanks to Leonard Pauli, the code worked perfectly in displaying the data but, it wouldn't update the database using my update.php. Below is the revised code and screenshot of what I was trying to archive.
Screenshot of single lined data displayed in 3 columns
<style>
body {
background:#5F5F6B;
color:#fff;
width:800px;
height:550px;
border:2px solid #bbb;
padding:20px;
float:center;
}
input[type="text"] {
width: 30px;
}
.table {
width:180px;
margin:1px;
border:2px solid #bbb;
padding:10px;
float:left;
}
.header {
width:595px;
margin:1px;
border:2px solid #bbb;
padding:10px;
float:left;
}
</style>
<div class="header"><b>Price List for dates from <font color ="yellow"><?php echo "$SPA"; ?> to <?php echo "$EPA"; ?></font></div>
<?php
$dataprice = $_POST['database'];
$datesrange = $_POST['id'];
$result = mysql_query("SELECT id, pounds FROM $dataprice ORDER BY id");
echo '<form name="prices" method="POST" action="update.php">';
$i = 0;
while ($rows = mysql_fetch_array($result)) {
echo '<div class="table">Day <font color="yellow">'.$rows['id'].' </font> costs ';
echo "<input type='hidden' name='id[$i]' value='{$rows['id']}' >";
echo "<input type='text' name='pounds[$i]' value='{$rows['pounds']}' > Pounds";
echo '<br/></div>';
$i++;
}
echo "<input type='hidden' name='databases' value='$dataprice'>";
echo '<center><input type="submit" value="Update Prices"/>';
echo '<center><font color="yellow"><br><br><br>IF UPDATING PRICE BAND D, ONLY ENTER THE VALUE OF WHICH
PRICES YOU WANT TO INCREASE BY, <br>EXAMPLE: 7 DAYS, IF CURRENT PRICE IS 30, IF YOU WANT TO
CHARGE 34, ONLY ENTER 4 AND LEAVE EVERYTHING ELSE SET TO 0</b></center>';
echo "</form>";
?>
A bit of an idiot really, completely forgot about CSS styling.
I spent the last 2 days trying to make the following code work.
I have three forms with content in different languages. For each of them there is an Edit button which popup a text editor. What I would like to do is when I click the button "Save and close" the edited content would be updated in the database using javascript (AJAX) and PHP/SQL but in the console it gives me the following error: Uncaught ReferenceError: valueContent is not defined. So here is the code I have so far.
$mvccont .=
"<form action='edit_language/".ID.".raw' method='post' target='pop' class='comparelist' style='clear: both' onsubmit='window.open(\"\", \"pop\", \"width=0,height=0,resizeable,scrollbars\");'>".
"<input type='hidden' name='keyword' value='".$keyword."'>".
"<table style='width:100%; border-collapse: collapse;' >".
"<tr><td width='20%' ><div class='info' style='width:100%; border:none;' >".
substr($keyword, 0, 20)."<a title='".$keyword."'><input type='submit' value='Save' class='BUYN'/></a>".
"<a href='edit_language/".$keyword."/delete'><input type='button' value='Delete' class='NONA'/></a>".
"</div></td>";
foreach($lan as $lang=>$lame) {
$idk = str_replace(" ", "", $keyword).$lang;
$mvccont .=
"<td><div class='number3'".(($red[$keyword][$lang]=='')?" style='background: #fcc;'":"")." style='border:none;' >".
"<input type='hidden' name='touch".$lang.$keyword."'/>".
"<input type='text' id='s".$idk."' name='a".$lang."' value='".htmlentities($lame, ENT_QUOTES, "UTF-8")."' onchange=\"this.style.borderColor='#ff0000'; document.getElementsByName('touch".$lang.$keyword."')[0].value='true'\"/><a onclick='popbox(false, ".$idk.", true); mce();'><input type='button' value='Edit' class='BUYN'/></a>".
"<br />".($red[$keyword][$lang]).
"<script>".
"function falaffel".$idk."(step) {".
"var l".$idk."=document.getElementById('s".$idk."').value;".
"if(step==1) {".
"return '<textarea>'+l".$idk."+'</textarea><input type=\"button\" value=\"Save & close\" class=\"NONA\" onclick=\"'+\"document.getElementById('s".$idk."').value=tinyMCE.activeEditor.getContent(); ".$idk."=falaffel".$idk."(1); popboxremove(); document.getElementById('s".$idk."').style.borderColor='#ff0000'; updateValue(); document.getElementsByName('touch".$lang.$keyword."')[0].value='true' \"+'\">';".
"}".
"return l".$idk.";".
"}".
"var ".$idk." = falaffel".$idk."(1); ".
"</script>".
"</div></td>";
}
$mvccont .=
"</tr></table></form>";
Then here is the AJAX code:
function updateValue(){
var ajaxRequest = getXmlHttpRequestObject();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var valueContent = document.getElementsByTagName("input");
}
}
ajaxRequest.open("POST", "comp/update_lang.php", true);
ajaxRequest.send(valueContent);
}
And finally the PHP code:
<?php
foreach(array(1=>"en", 2=>"it", 3=>"pl") as $id=>$lang) {
if($_POST["touch".$id.$_POST["keyword"]]=="true") {
$sql = "UPDATE translat SET value='".mysql_real_escape_string($_POST["a".$id])."', date=".NOWTIME." WHERE lang=".$id." AND keyword='".urldecode($_POST["keyword"])."'";
$insert = $db->query($sql);
$insert->execute();
#unlink(TEMP."lang_".$lang.".cache");
}
}
$mvccont .=
"<script>".
"window.close();".
"</script>";
die($mvccont);
?>
Thank you so much for your help.