Jump to content

How to post the right data?


Terminaxx

Recommended Posts

Hey guys,

i got something similiar like a shop, not for others, just for me to try things out.

Therefore I was trying to be able to change the price on my own:

<?php                           
 $c = 0;
 foreach ($furnis as $f) {
   if ($c >= $limit and $c <= ($limit + 40)){
   echo ".$f->name.";
   echo "<b>Price:</b> ".number_format($f->price, 0, ",", ".")." Taler<br>";
   echo "<b>Quantity:</b> ".$f->quantity."<br>";
   echo "<input name='newprice' value='$f->price' type='number' onkeydown='return keyispressed(event);' min='1'>";
   echo "<button type='submit' name='changeprice' value='$f->id'>Chance price</button>";

  }
$c++;
}
?> 
<?php    
if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if(isset($_POST["changeprice"])){

            $id = input($_POST['changeprice']);
            $row = mysqli_fetch_object(mysqli_query($con, "SELECT * FROM shop WHERE id = $id"));
            if (!empty($row)){
                $furniname = $row->name;
                $price = $row->price;
                $owner = $row->number;
                $quantity = $row->quantity;
                $newprice = $_POST['newprice'];
                
                if($newprice >= 1 AND $newprice <= 80000000) {
                    mysqli_query($con, "UPDATE shop SET price = '$newprice' WHERE id = '$id'");

                    $correct = "Price changed";
                }elseif($newprice == 0){
                    $error = "No new value detected";
                }elseif($newprice > 99999999) {
                    $error = "New price is too high";
            }else{
                $error = "Furni doesn't exist";
            }
        }
    }
    }
?>

So this worked fine as long as I only got 1 item to change. If on the site there were more than one shown, it didn't work anymore.

I think it is because of the "$newprice = $_POST['newprice'];" That it detects all prices from every furni, eventho I only clicked on one to change.

So I tried it with "value='$f->price'", therefore I thought If I haven't entered anything in a column, the old price is taken over. But it doesn't seem so.

 

I have to change the price from every furni shown, then click on the "Change Price - button" to change the ONE price, i clicked on.

 

Is there any solution?

 

Sorry for my poorly english, i hope you still understand what i mean.

 

Here's how it looks like:

http://prntscr.com/fqcii8

 

If I now enter a number in either of them, the price would not change.

If I would enter a number in both and click Change price, the price changes from the selected one.

 

 

Thanks for any help

 

Link to comment
Share on other sites


<html lang="de">
<head>
<title>Shop</title>
<script>
function keyispressed(e){
var charCode;
if (e.keyCode > 0) {
charCode = e.which || e.keyCode;
}
else if (typeof (e.charCode) != "undefined") {
charCode = e.which || e.keyCode;
}
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
</head>

<body>
<form method="post">
<?php echo $error;?>
<?php echo $correct;?>

<?php
if(isset($_POST["searcharticlename"])){
$search = input($_POST["searcharticlename"]);
$res = mysqli_query($con, "SELECT * FROM shop WHERE name LIKE '%$search%' AND number = '$number'");
}else{
$res = mysqli_query($con, "SELECT * FROM shop WHERE number = '$number'");
}

$count = countfurni($res);
$sites = ceil($count / 40);

if (empty($_GET['site'])) {
$site = 1;
} else {
$site = $_GET['site'];
if ($site > $sites) $site = 1;
}

$limit = ($site * 40) - 40;
?>


<?php
if(isset($_POST["searcharticlename"])){
$search = input($_POST["searcharticlename"]);
$res = mysqli_query($con, "SELECT * FROM shop WHERE name LIKE '%$search%' AND number = '$number'");
}else{
$res = mysqli_query($con, "SELECT * FROM shop WHERE number = '$number'");
}

$furnis = array();
$names = array();
while($row = mysqli_fetch_object($res))
{
$id = $row->id;
$img = $row->img;
$owner = $row->number;
$name = $row->name;
$price = $row->price;
$quantity = $row->quantity;

if(!in_array($name, $names)) {
$f = new furnipack();
$f->img = $img;
$f->quantity = $quantity;
$f->name = $name;
$f->price = $price;
$f->id = $id;

array_push($furnis, $f);
array_push($names, $name);
}else{
foreach ($furnis as $f) {
if($f->name == $name){
$f->quantity += $quantity;
if($price < $f->price){
$f->price = $price;
$f->id = $id;
}
}
}
}
}

$c = 0;
foreach ($furnis as $f) {
if ($c >= $limit and $c <= ($limit + 40)){
echo ".$f->name.";
echo "<b>Price:</b> ".number_format($f->price, 0, ",", ".")." Taler<br>";
echo "<b>Quantity:</b> ".$f->quantity."</span><br>";
echo "<input name='newprice' value='$f->price' type='number' >";
echo "<button type='submit' name='changeprice' value='$f->id'</button>";
}
$c++;
}
?>
</form>
</body>
</html>
Link to comment
Share on other sites

Must be a bad day.  I see a lot of php, some JS, some horribly convoluted spaghetti code and the bare beginnings of an html form, but no real form itself for me to determine what your POST input is going to resemble.

 

Should I give up?

Link to comment
Share on other sites

The code above isn't important, because it doesn't affect it at all. The input just makes clear, which ID it has to change. This works just fine. My only problem is this line:

 echo "<input name='newprice' value='$f->price' type='number' >";

Since this is the only NEW code which i add to the previous one.

I dont understand how to make it work, that when I write the "newprice" inthere, I don't have to write a price for every other article on the page.

Link to comment
Share on other sites

So this worked fine as long as I only got 1 item to change. If on the site there were more than one shown, it didn't work anymore.

 

Since all the <input> tags are in the same <form> tag, the name attributes need to be unique. Otherwise, PHP is only going to receive the value from the last "newprice" text box.

 

Assuming that $f->id contains the unique ID of the item, you could try something like this

echo "<input name='newprice[$f->id]' value='$f->price' type='number' >";
 
Then $_POST['newprice'] would contain an array of prices where the item IDs are the array keys.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.