Wrong value of input number returned - php

I'm doing this assignment where I have to do website. I created a input number for the quantity of the product with a button all this in a form. When I try to get the quantity by putting in the URL. The problem is I keep getting the wrong quantity. Not the quantity that I had in my input.
<?php
foreach ($productList as $product){
?>
<form action="index.php" method="post">
<?php echo "<ul class='row'>"?>
<?php echo "<li>"?>
<?php echo "<img src ='".$product->getImage()."'class=image'>" ?>
<?php echo "<div class='overlay'>"?>
<?php echo '<div class="text">'.$product->__toString()."</div>"?>
<?php echo "</div>"?>
<?php
echo"<p id ='titleQte'>Quantity </p>";
echo"<div class='qteProduct'>";
echo "<input type='number' name='qte' value='1' min = '1'/>";
echo"</div>";
echo"<a href=index.php?action=addCartProduct&id=".$product->getId().'&quantity='.((isset($_POST['qte'])) && (isset($_POST['btnAjout'])) ? $_POST['qte']:1).'>'."<button type='button' name='btnAdd' id='add' class='btn btn-success'>Add to Cart</button></a>";
?>
<?php echo "</li>"?>
<?php echo "</ul>"?>
</form>
<?php
}
?>
It gives me 1 instead of the actual value of the input.
Some help would be appreciated.
Thank you.

You are passing the value in the tag as value = '1' so this takes precedence over the actual input. Remove it and pass
echo "<input type='number' name='qte' min = '1'/>";
Hope this helps !

Just remove the value attribute from this line:
echo "<input type='number' name='qte' value='1' min = '1'/>";
into
echo "<input type='number' name='qte' min = '1'/>";
I hope this would be helpful.

Related

How to pass a textbox value in to url parameter

i have some problem in here. I have textbox value inside table, and when i clicked link "Valid", i can pass the textbox value to another page.
This is my textbox code in table
echo "<form name='nomor' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "</form>";
This is my button
echo "<td class='center'>";
echo "<a class='btn btn-primary btn-sm' href='validasi.php?idorder=" . $row['id_order'] . "&pilih=" . $_GET['pilih'] . "'>
<i class='fa fa-check-square-o'>Valid</i></a>
echo "</td>";
Maybe someone can give me a solution, Thank you and have a nice day!!
In the page validasi.php add this:
$IDorder = $_GET['idorder'];
$Pilih = $_GET['pilih'];
Now you have two variables on validasi.php called IDorder and Pilih from your form
Or is it the nomortempat you want?
I think you can just add
$nomortempat = $_GET['nomortempat'];
to the validasi page and it should work. If I understand your code it should be sent
EDIT I was wrong, you need to add a sumbmit button to your form.
Add session start at the top of your pages: session_start();.
Then add this to your first page:
$_SESSION["idorder"] = $idorder;
$_SESSION["pilih"] = $pilih;
Then this code to your form:
echo "<form name='nomor' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "<input type='submit' name='submit' value='Submit'></input>";
echo "</form>";
Then on validasi page add this:
$nomortempat = $_GET['nomortempat']; // and:
$IDorder = $_SESSION["idorder"];
$Pilih = $_SESSION["pilih"];
Now you have all three values on validasi.php
with GET form, you could try this:
echo "<form name='nomor' action='action.php' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "<input type='submit' value='valid'>";
echo "</form>";
and in action.php page, to catch value in textbox name='nomortempat', code is below:
if (isset($_GET['nomortempat'])){
echo $_GET['nomortempat'];
}
Of course, you could add more input fields whenever you want to inside form with different names.

Display Contents of Textbox That was created by PHP

I would like some help printing what is in a certain textbox that was created by an echo command.
while($row = $result->fetch_assoc()){
$stringTest = $row['Price'];
$AssetId = $row['AssetId'];
echo "<center><div> <h3>Cost: ".$stringTest."";
echo '<form action="" method="get"><input type="text" name="uid">';
echo "</br><input class='myButton' type='submit' Name='Submit1' VALUE='I have bought'></a></form>";
/** ^ Input value I would like to get *//
echo "<a href='https://www.roblox.com/item-item?id=".$AssetId."' class='myButton'>Buy</a></h3></div></center>";
}
Use the code below to get the value from submit:
if(isset($_GET['Submit1'])) {
echo $_GET['Submit1'];
}
When the user clicks submit, it will echo the value of it.
If you want to print PHP element in a textbox you should put it in the value tag of the input
<?php
echo "<input type='text' value='" . $val . "'>";
?>

Dynamically changing form action

I'm trying to change the form action on the fly, but think I'm having a syntax problem, that I just can't figure out.
This works:
<?php
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action='linktogoto.php'\">";
?>
This doesn't work:
<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action=$variable\">";
?>
I need to eventually change $variable depending on the situation, so need to get this to work.
<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action=$variable\">";
?>
Should be:
<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action='$variable'\">";
?>
Notice the ' around $variable.

another simple PHP page

I'm trying to create a simple PHP page that will Display a number starting with 0. It will then ask for a number input, then add the inputted number to the beginning number. and keep adding the number that is input in the text box. Here's what I have so far... I can get the first variable initialized and get the input from a form box. I just can't seem to add them together and keep a running total. Thanks in advance.
<?php
$_POST['number1'];
echo "Current number is ".$_POST['number1'];
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number2']."<br>";
$sumtotal = $_POST['number1'] + $_POST['$number2'];
echo "Your new total is ".$sumtotal;
$_POST['number1'] == $_POST['number2'];
?>
Using input type HIDDEN:
<?php
if(isset($_POST['submit'])){
$total=$_POST['number1']+$_POST['number2'];
echo "Your entered number is ".$_POST['number2']."<br>";
}
else {
$total=0;
}
echo "Current total number is ".$total;
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='hidden' name='number1' value='$total'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$total;
?>
Using SESSION:
<?php
session_start();
if(empty($_SESSION["total"])){
$_SESSION["total"]=0;
}
if(isset($_POST['submit'])){
$total=$_SESSION["total"];
$total=$total+$_POST['number2'];
$_SESSION["total"]=$total;
echo "Your entered number is ".$_POST['number2']."<br>";
}
echo "Current total number is ".$_SESSION['total'];
echo "<br>";
echo "Enter your number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number2'>";
echo "<input type='submit' name='submit' value='Submit'>";
echo "</form><br>";
echo "Your new total is ".$_SESSION['total'];
?>
Both works and have the same output, but with different approach from one another.
You could add another hidden input field that keeps the last number that was posted by you.
<input style="display: none" name="tmp_number" value="<?php echo $tmp_number; ?>">
Try (edited for all details and errors)
<?php
$input = $sum = 0;
if (isset($_POST)) {
$input = $_POST['number1'];
$prev_sum = $_POST['prev'];
$sum = $input + $prev_sum;
}
echo "Current number is ".$input;
echo "<br>";
echo "Enter your next number.<br>";
echo "<form action='' method='POST'>";
echo "<input type='number' name='number1'>";
echo "<input type='hidden' name='prev' value='$sum' >";
echo "<input type='submit' name='submit' value='Submit'>";
echo "<br>";
echo "Your entered number is ".$_POST['number1']."<br>";
echo "Your new total is ".$sum;
?>

get a variable row from a while mysql_fetch array that isnt the last result

Hey guys I'm having a hard time sending a session variable of the $row['projectnaam']
that belongs to the one that just has been clicked.
At the moment the Session always takes the last $row['projectnaam']
from the while loop and I'm wondering how I can send the right
variable with a session that belongs to the row that just has been clicked.
Thank you in advance.
Here's my syntax:
<?php
include "config.php"
$bedrijfsnaam = $_SESSION['gebruikerbedrijf'];
$result= mysql_query("SELECT * FROM projecten WHERE bedrijfsnaam='$bedrijfsnaam' ")or die(mysql_error());
while($row = mysql_fetch_array($result)){
if (isset($_POST['submit'])){
$_SESSION['projectnaam'] = $projectnaam;
header('Location: viewprojectsbedrijf.php');
}
echo "<div class='project'>";
echo "<div class='projectdetails'>";
echo "<p class='projectnaam'>";
echo $row['projectnaam'];
echo "</p>";
echo "<hr class='paars'>";
echo "<p class='datum'>";
echo $row['datum'];
echo "|";
echo $row['Tijd'];
echo "</p>";
echo "<hr class='paars'>";
echo "<p class='bedrijfsnaam'>";
echo $row['bedrijfsnaam'];
echo "</p>";
echo "</div>";
echo "<div class='view'>";
echo "<form method=\"POST\" action=\"\">";
echo "<input type='submit' value='View' name='submit' class='viewbutton'></input>";
echo "</form>";
echo "</div>";
}
?>
Mede Nederlander ;)
I think you mean to do something like this:
include("config.php");
if(isset($_POST['submit'])){
$_SESSION['projectnaam'] = $_POST['projectnaam'];
header('Location: viewprojectsbedrijf.php');
}
$bedrijfsnaam = $_SESSION['gebruikersbedrijf'];
$result= mysql_query("SELECT * FROM projecten WHERE bedrijfsnaam=".$bedrijfsnaam)or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<div class='project'>
<div class='projectdetails'>
<p class='projectnaam'>
<form method=post action=''>
<input type=text value=".$row['projectnaam']." name=projectnaam>
</p>
</div>
</div>
<input type='submit' value='submit' name='submit'>
</form>";
}
In your own script only your submit button is in the form, so that will be the only value posted. In that case you won't get the POST value of 'projectnaam'.
Also, you try to make a SESSION value of a variable which isn't declared in your posted code. In my code it makes a Session value of the POSTED code.

Categories