I have trouble when creating code for adding an item to shopping cart. What I did is echo each individual item customers could buy, and I wanted to add an input type for how many of that specific item they want, then "add to cart" button and repeat (echo another item, another input and button etc.)
while ($zaznam = mysqli_fetch_assoc($vysledek)) {
$soubor=$zaznam["img_nazev"];
echo "<div id='zbozi_info'>";
echo "<img src='img/$soubor'>";
echo "<p class='popis'>".$zaznam["dlouhy_popis"]."</p>";
echo "<p class='cena'> Cena bez DPH: ";
echo "<span style=color:#FF0000>".$zaznam["cena"]." Kč </span> </p>";
echo "<p class='cena'> Cena s DPH: ";
echo "<span style=color:#FF0000>".$zaznam["CenaDPH"]." Kč </span> </p>";
echo "</div>";
echo "<form method='post'>
<label for='pocetk'>Počet kusů</label>
<br>
<input type='text' name='pocetk' id='pocetk' value='1'>
<br>
<input type='submit' name='pridkos' id='pridkos' value='Přidat do košíku'>
</form>";
}
This works and all, the trouble starts when I add this if to the mix, to enable that adding function
if (isset($_POST["pridkos"]) && isset($_SESSION['user'])) {
$ksk="INSERT INTO kosik
(ID_uzivatele,ID_zbozi, pocet_kusu)
VALUES ('".$_SESSION['user']."',
'".$zaznam["ID_zbozi"]."',
'".$_POST["pocetk"]."');";
$vysledek = mysqli_query($con,$ksk)
or die("Zboží nebylo přidáno do košíku");
This just dies on the mysqli_query. I am not exactly sure if to put it inside the while or outside, my suspicion is inside, but it isn't working either way
Session[user] has the user ID (ID_uzivatele), not sure what else can I clarify.
Any help is appreciated.
I think this query syntax is wrong.
Check with below one:-
$ksk="INSERT INTO kosik(ID_uzivatele,ID_zbozi, pocet_kusu)
VALUES ('$_SESSION[user]',
'$zaznam[ID_zbozi]',
'.$_POST["pocetk"]')";
Related
i searched previous sample questions but those are not helpful my question..
My task is i have several types of rooms in my hotel. when i select the rooms, after submit the form the room numbers and price details should be store into redirected page.
i use this command
here I modified my code like below
<?php while($row24 = mysql_fetch_array($result24)):;?>
<div class="item-gallery isotope-item bo-rad-10 hov-img-zoom triple">
<?php echo "<img src='images/$row24[pic]' alt='IMG-GALLERY'>"?>
<div class="overlay-item-gallery trans-0-4 flex-c-m">
<?php echo "<a class='btn-show-gallery flex-c-m fa fa-search' href='images/$row24[pic]' data-lightbox='gallery'></a>"?>   
<?php echo "<label class='container1 txt17'>Select Room
<input type='checkbox' name = 'check_list[]' value='$row24[roomno] ,$row24[price]'>
<span class='checkmark'></span>
</label>"?>
</div>
<div class="line-item-mainmenu text-blo3 size21 flex-col-l-m">
<?php echo "<span class='txt20 m-b-3'>" .'Room No : '
.$row24['roomno'].
"</span>"?>
<?php echo "<input type='text' class=' txt16 m-b-3'>".'Price       : '
.$row24['price']."   <i class='fa fa-rupee'></i>"."   (One Night )" ?>
</div>
</div>
<?php endwhile;?>
by using this code i get he results which i expect. but my problem is how to extract price from that array
When including object/array elements in strings, you need to wrap them with {} so they will be parsed properly.
For example
<?php echo "<img src='images/$row24[pic]' alt='IMG-GALLERY'>"?>
should be
<?php echo "<img src='images/{$row24[pic]}' alt='IMG-GALLERY'>"?>
Aside from that, it does not matter what room is selected, all these form fields (minus those unchecked input boxes) are going to be submitted and you'll have a tough time trying to parse the data out.
If, at the very least, assign roomno as an associative key for each of your fields, for example:
<input type='checkbox' name = 'check_list[]' value='{$row24[roomno]}'>
<input type='text' name='price[]' value='{$row24[roomno][price]}' />
That way, at least you can iterate through the $_POST, price field(s) based on the roomno key selection(s).
I have two .php pages, one that returns the contents of a table including an id for each entry along with an html text input and another that retrieves the details and allows me to update the record.
I'd like to store the id of the entry by clicking on the list item using a href rather than having to text input the id and submit.
choose.php
echo "<ul>";
while ($row=mysql_fetch_array($result)) {
$reference=$row[reference];
$name=$row[name];
echo "<li>$reference, $name</li>";
}
echo "</ul>";
session_start();
$_SESSION['regName'] = $reference;
mysql_close($link);
?>
<form method="get" action="update.php">
<input type="text" name="regName" value="">
<input type="submit">
</form>
update.php
session_start();
$reference = $_GET['regName'];
echo "Your selection id is: ".$reference.".";
$query="SELECT * FROM firsttable WHERE reference='$reference'";
$result=mysql_query($query) or die("Query to get data from firsttable failed with this error: ".mysql_error());
$row=mysql_fetch_array($result);
$name=$row[name];
echo "<form method=\"POST\" action=\"updated.php\">";
echo "<p>";
echo "<label for=\"name\">Name: </label><input type=\"text\" id=\"name\" name=\"name\" size=\"30\" value=\"$name\"/>";
echo "<p><input type=\"submit\"></p>";
echo "</form>";
I apologise if this seems very obvious, I've only started to learn php as of today and it's much more complicated than anything I've done up until now.
Thanks
James
Answering your question, you just need to use HREF parameter of A tag. This will make an active link, which will contain a reference you need:
echo '<ul>';
while ($row = mysql_fetch_array($result)) {
$reference = $row[reference];
$name = $row[name];
echo '<li><a href=/update.php?regName='.$reference.'>'.$name.'</a></li>';
}
echo '</ul>';
?>
Read here for more details about passing data via GET\POST: https://www.w3schools.com/tags/ref_httpmethods.asp
And as you were told above, please consider rewiring the code as it is totally unsafe and must not be used anyhow besides some very basic concept proof. And only inside intranet or local machine.
Good luck!
I'm try to build a snack machine where you can choose your snack, get the price and then click the button to pay.
Like:
The price is 0,60 €
0.05(button) 0.10(button) ....
if you press 0.05-button the price will reduce to 0,55€
Why don't I get a "test" echo after I click the button?
Here is my code
<?php
if(isset($_GET['mars']))
{
$mars = "0,60";
echo "Bitte Zahlen Sie noch <input type=\"button\" value=\"$mars\"> Euro<br>";
echo "<input type=\"submit\" value=\"0,05\" name=\"fcent\">";
if(isset($_GET['fcent']))
{
echo "test";
}
}
?>
First, there appears to be no form-tag in your code. Without a form tag, it would be a miracle that pressing that button actually submits it via PHP. In other words, you need to wrap your form elements in a <form></form> Tag. Secondly, the nested if: if(isset($_GET['fcent'])) is unreachable because when you press the fcent button; the $_GET['mars'] is no more in scope and since your code explicitly demands to be run when $_GET['mars'] is SET, nothing would happen. The Snippet below takes this 2 Points into account and you can fine-tune it even further to meet your needs...NOTE: You have to be sure that your URL reads something similar to this: http://localhost/index.php?mars=some-value
<?php
$mars = "0,60";
$payForm = "<form name='whatever' method='get' action=''><br>";
$payForm .= "Bitte Zahlen Sie noch <input type=\"button\" value=\"$mars\"> Euro<br>";
$payForm .= "<input type=\"submit\" value=\"0,05\" name=\"fcent\">";
$payForm .="</form>";
if(isset($_GET['mars'])){
echo $payForm;
}
if(isset($_GET['fcent'])){
echo $payForm;
echo "test";
}
I have made a php page. On which I am displaying table 'prod' from my data base.
each row is displayed nicely. Today i tried to add a button named 'rate' at the end of each row of my table. which I did successfully. Now I want to send the the value of the first column of that row to another php page when that button is clicked. I am stuck that how to do so? can you help please ??
I know i have to use the method post in my form and i have to use $_post[that value] on the other php page to inculcate the value for further function.
I just need to ask that where to add the value of my first column in the button line. so that onclick it can send that value. I hope I am clear over this. Thank You very much for help :)
<?php
include("connection.php");
$query = "select * from prod";
$res = oci_parse($conn,$query);
usleep(100);
if (oci_execute($res)){
usleep(100);
print "<TABLE border \"1\">";
$first = 0;
while ($row = #oci_fetch_assoc($res)){
if (!$first){
$first = 1;
print "<TR><TH>";
print implode("</TH><TH>",array_keys($row));
print "</TH></TR>\n";
}
print "<TR><TD>";
print #implode("</TD><TD>",array_values($row));
print "</TD></TR>\n";
echo "<td><form action='detailform.php' method='POST'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
}
print "</TABLE>";
}
?>
you have to add inputs in your form whatever kind u prefer
echo "<td>
<form action='detailform.php' method='POST'>
<input type='hidden' name='your_val_key' value='".$row[your_val_key_in_query]."'> <!-- input hidden, change to text 4 debug -->
<input type='submit' name='submit-btn' value='Rate'/>
</form>
</td></tr>";
and than, in your detailform.php u can get the val with
echo $_POST["your_val_key"];
if u are not sure how much data u send or somthing, try this and u get the full data:
echo "<pre>".print_r($_POST,true)."</pre>";
BTW: why are u mixing print and echo?
Use hidden input
echo "<td><form action='detailform.php' method='POST'><input type='hidden' name='col-name' value='you-col-value'><input type='submit' name='submit-btn' value='Rate'/></form></td></tr>";
For the last 4 hours I've been struggling to get something to work. I checked SO and other sources but couldn't find anything related to the subject. Here is the code:
<?php
$email=$_SESSION['email'];
$query1="SELECT * FROM oferte WHERE email='$email'";
$rez2=mysql_query($query1) or die (mysql_error());
if (mysql_num_rows($rez2)>0)
{
while ($oferta = mysql_fetch_assoc($rez2))
{
$id=$oferta['id_oferta'];
echo "<input type='radio' name='selectie' value='$id' id='$id'> <a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo "</br>";
}
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
echo "</form>";
echo "</div>";
}
else
{
}
?>
The while drags all of the user's entries from the database and creates a radio button for each one of them with the value and id (because I don't really know which one is needed) equal to the entry's id from the db. I echoed that out and the id is displayed as it should so no problems there.
The delete script works ok as well so I won't attach it unless you tell me to. All good, no errors, until I try to delete an entry. Whatever I choose from the list of entries, it will always delete the last one. Note that I have two other inputs echoed out, those will be the "view" and "modify" buttons for the entry.
I really hope this is not JavaScript related because I have no clue of JS. I think this will be of major help to others having this problem. Please let me know if I need to edit my question before downrating. Thanks!
After edit:
This is the delete script, which as I said earlier works fine.
<?php
if (isset($_GET['id']))
{
$id = $_GET['id'];
echo $id;
require_once('mysql_connect.php');
$query = "DELETE FROM oferte Where id_oferta = '$id'";
mysql_query($query) or die(mysql_error());
//header('Location: oferte.php');
}
else
{
//header('Location: oferte.php');
}
?>
The session is started as well, like this:
<?php
session_start();
?>
The reason the last $id is deleted is because this line is outside/after the while loop:
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
You want to move this line inside the loop so that you have a button that executes delete for each radio button.
Update:
To have links to delete and
echo "<input type='radio' name='selectie' value='$id' id='$id'> ";
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
Also I do not think the radio button is needed here at all since you are not really doing anything with it. You could simply echo out the value of your choice and have these links as follows:
echo $oferta['denumire_locatie'] . ' '; // replace $oferta['denumire_locatie'] with something of your choice
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a> ";
echo "<a href='delete.php?id=$id'>delete</a>";
echo "<br />";
The problem, in this case, is JavaScript related, yes. What I recommend you to do is to simply add a Remove link for each item.
echo "<a href='oferta.php?id={$oferta['id_oferta']}'>{$oferta['denumire_locatie']}</a>";
echo " - <a href='delete.php?id={$oferta['id_oferta']}'>Remove</a>";
echo "</br>";
Your $id is outside your while() loop.
The last one is getting deleted because the $id has the last one's value when the loops is exited.
Include all your code :
echo "</br>";
//echo "<input type=\"button\" id=\"cauta\" value=\"Vizualizeaza\" onclick=\"window.location.href='oferta.php?id={$oferta['id_oferta']}'\" />";
//echo " <input type=\"button\" id=\"cauta\"value=\"Modifica\" onclick=\"window.location.href='modifica.php?id={$oferta['id_oferta']}'\" />";
echo " <input type=\"button\" id=\"sterge\" value=\"Sterge\" onclick=\"window.location.href='delete.php?id=$id'\" />";
Inside your while loop.
When the rendered html reaches the browser, it will be something like this:
<input type='radio' name='selectie' value='1' id='1'> <a href='oferta.php?id=1'>TEXT</a>
<input type='radio' name='selectie' value='2' id='2'> <a href='oferta.php?id=2'>TEXT</a>
<input type='radio' name='selectie' value='3' id='3'> <a href='oferta.php?id=3'>TEXT</a>
<input type='radio' name='selectie' value='4' id='4'> <a href='oferta.php?id=4'>TEXT</a>
<br/>
<input type="button" id="sterge" value="Sterge" onclick="window.location.href='delete.php?id=5'" />
With this markup you won't be able to accomplish what you want without using javascript to update the onclick attribute whenever you select a radio button.
On the other hand, instead of using the client-side onclick event you can use the button's default behaviour, which is to submit the form.
You'll just have to set the action attribute:
<form method="post" action="http://myurl.php">
and write the myurl.php page which will just read the posted variable $_POST['selectie'] and call the delete method with the posted id.