PHP Get and POST data - php

How to I get the input id of selected button, and post it from next php page? I have this code inside my form it's dynamically populate from my database.
<?php
include('connection.php');
$query = "Select * from tblproduct where categoryID = 1 and statusProd = 1";
$result = $conn->query($query);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id=$row["ID"];
$product=$row["product"];
$image=$row["images"];
echo '
<div class="col-sm-4 divProduct" style="outline: none;background-color: transparent;border:none;height:320px;width:300px">
<div class="content">
<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="">
<input type="hidden" name="prodID" value="'.$id.'">
<div class="btnBuyBuy text-center">
<img src="'.$image.'" class="imgProd" style="width:200px;height:170px;">
<br><br>
<label class="nameProd">'.$product.'</label>
<br><br>
<div class="divBuy2">
<label class="lblBuy">BUY NOW</label>
</div>
</div>
</div>
</div>';
}
}
And here's my code to the next page where the id displaying.
<?php
if (isset($_POST['btnGame'])) {
$id = $_POST['prodID'];
echo "$id";
}
else{
echo "failed";
}
And the output is the last id from my tblproduct which is 13. How can I get the id of selected button?

<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="">
<input type="hidden" name="prodID" value="'.$id.'">
You aren't trying to get the "id" (you mean value) of the button, you are trying to get the value of the hidden input next to it.
The problem with this is that proximity to the clicked submit button means nothing. All hidden inputs will be successful controls. All will be submitted to the server. Since they all share the same name, only one of them will show up in $_POST. (If you renamed them so the name ended in [] then they would all should up as an array and you still couldn't tell which was selected).
Don't use a hidden input for this.
If you care about the button that is used, then make use of the submit button.
Only the submit button used to submit the form will be successful, so its name and value can be used to tell which one was clicked.
<input type="submit" name="btnGame" id="btnGame" class="btnSubmit" value="$id">
… and then look at $_POST['btnGame'] instead of $_POST['prodID'].
Given that you have value="" I'm guessing that you don't want any text displayed on the image and that you are using CSS background image to display something in it.
Obviously, the above won't be compatible with this, so use a <button> element instead.
That allows you to have a different label and value.
It also allows you to put elements inside the label, so you can use a content image with an alt attribute instead of a background image and score a big accessibility win.
<button name="btnGame" id="btnGame" class="btnSubmit" value="$id">
<img src="/path/to/icon.png" alt="Select product number $id">
</button>

Related

How can I call dynamic ISSET by multiple ID button?

As you can see in the picture I have multiple button inside one FORM so when I click on button It insert always the last value :: S (126).
This is my code:
<form method="post" action="" enctype="multipart/form-data">
<input type="text" class="form-control" placeholder="Numéro de chassit" name="chassit" id="chassit" >
<?php
if (mysqli_num_rows($result) > 0)
{ ?>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<?php echo "<img alt='' src='../admin/image/mercedes/".$row['image']."' >";?>
<div class="card-body">
<input id="model" name="model" type="hidden" value="<?=$row['model'] ?>">
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</div>
</form>
And this is the process
<?php
if(ISSET($_POST['mo'])){
$demandeur = $_SESSION['username'];
$model = $_POST['model'];
$chassit = $_POST['chassit'];
$sql = "INSERT INTO tbl_xp_support (demandeur,model,chassit)
VALUES ('$demandeur','$model','$chassit')";
if (mysqli_query($con, $sql)) {
header("Location: ../xp_group.php");
} else {
echo "Error: " . $sql . " " . mysqli_error($con);
}
mysqli_close($con);
}
?>
The input name="model" it insert always the last value !!!
Because your all input fields has same name, so last one will overwrite any previous ones. If you want to select only one that was selected, you should make it as radio button:
<label class="card-body">
<input id="model" name="model" type="radio" style="display: none;" value="<?=$row['model'] ?>"/>
<input type="submit" name="mo" class="btn btn-secondary" value="<?=$row['model'] ?>">
</label>
Please note:
you never use $_POST['mo'] value that should contain selected model
You have same ID in every iteration, and that is invalid HTML
Your browser has no way to know that each submit button is supposed to be associated with a single hidden input, since they're all part of the same form. What will actually happen is that regardless of which button is pressed, all of the hidden fields will be submitted.
Since they all have the same name, but different values, PHP has to decide which value to actually put into the $_POST array. Its policy is to take the last value, which is what you see.
There are a few ways I can think of to make this work:
Put each hidden field and submit button into its own HTML <form>, rather than having one for the whole page.
Have a single hidden field at the bottom of the page, and write some JavaScript that updates that hidden field when you click a button, before submitting the form.
Look at the value of the submit button by examining the $_POST['mo'] variable, and get rid of the hidden fields completely. But beware that there are some extra cases you need to consider when doing this.

Form auto calculate answer to php

I have an autocalc running on a form and the total is shown through a div ID. I need to be able to somehow render the div ID answer into a php form mailer.
I’ve tried adding a hidden input field with the div ID.
<div id="totalPriceTM">
<input name="Score" id="totalPriceTM" value="" type="hidden" />
</div>
No error messages but result not showing in email.
You should create different div name on your form.
for example :
<script>
function autocalc(x,y)
{
a = x*y;
document.getElementById(totalPriceTM).innerHTML = a;
document.getElementById(totalPriceTM2).value = a;
}
</script>
<div id="totalPriceTM">
<input name="Score" id="totalPriceTM2" value="" type="hidden" />
</div>
result will be show on div totalPriceTM and will be set the value of div totalPriceTM2

PHP foreach executing code multiple times

At the bottom of this code you'll see an 'Accept Offer' button, when I click on that another piece of code gets executed as you can see on the bottom of this post.
For example this project has 3 bidders, so 3 times bidder_id and writer_bid so I use 'foreach' and load it in divs, works fine, but now I need to store those variables in a database, which technically works but it doesn't store the bids from the row I pull them from, it just takes the data from the last row, that is if I place the code at the bottom of this thread in my header.
However when I put it inside the loop it executes three times, I saw that when I got an error message that I had to close 3 times cause there are 3 rows in the database table that I pull the data from.
How can I prevent this, and either have it load once when the code is inside the foreach loop, or have it pull the correct writer_bid and bidder_id to store.
<div class="WB-Bottom-block lefts">
<?php $getBidders=" AND project_id=$project_id"; $bidders=getBidder($getBidders); foreach($bidders as $bidder) {
$bidder_id=$bidder['writer_id'];
$writer_bid=$bidder['writer_bid'];
?>
<div class="findwriters_boxes">
<div class="findwriters_right">
<div style="float:right;margin-top:6px;width:170px;">
<input type="hidden" name="writer_bid" id="writer_bid" value="<?php echo $writer_bid; ?>" />
<input type="hidden" name="bidder_id" id="bidder_id" value="<?php echo $bidder_id; ?>" />
<input type="submit" class="homebtn11" name="submit" id="submit" value="Accept Offer"/>
</div>
</div>
</div><?php } ?>
Below the code that needs to be executed and that results in issues, whether I place it inside the foreach loop, or inside the header instead.
As you can see I tried to store it in input fields so that it stays there so the header can pull it on refresh of the page / click of the button.
<?php if(isset($_POST['todo']) && $_POST['todo']=='submit_project') {
$balance=get_client_balance_info($current_user->ID);
$writer_bid=$_POST['writer_bid'];
$bidder_id=$_POST['bidder_id'];
if($balance >= $_POST['writer_bid']) {
global $wpdb;
$sql3="UPDATE `wp_project` SET `writer_id` = '".$bidder_id."' WHERE `id` =". $project_id;
$wpdb->query($sql3);
$sql4="UPDATE `wp_project` SET `price` = '".$writer_bid."' WHERE `id` =". $project_id;
$wpdb->query($sql4);
$sql5="UPDATE `wp_project` SET `status` = '2' WHERE `id` =". $project_id;
$wpdb->query($sql5);
$success_msg="You accepted a bid, the money will be deducted from your account.";
}
else $fail_msg="Your balance is not sufficient.";
I think you should make a form for each div that you are adding right now you are putting the bidder_id in the different inputs but the same name.
So it will get the last inputs, maybe it's better to specify the inputs with the row id or to separate the forms or make the input names as array.
I hope this helps you.
I fixed it with the help of Diar Selimi like this:
<div style="float:right;margin-top:6px;width:170px;">
<form action="" name="frmeditor" method="post" id="frmeditor" >
<input type="hidden" name="todo" id="todo" value="submit_project" />
<input type="hidden" name="writer_bid" id="writer_bid" value="<?php echo $writer_bid; ?>" />
<input type="hidden" name="writer_id" id="writer_id" value="<?php echo $writer_id; ?>" />
<input type="submit" class="homebtn11" name="submit" id="submit" value="Accept Offer"/>
</form>
Before that my form and value="submit_project" tags were scattered all over the place!

Radio option selected so post to this page

i have been trying to find my way around this issue, I have used
$(function(){
$('form').submit(function(event){
event.preventDefault();
window.location = $('input[type=radio]:checked').val();
});
});
but all that does is redirect me to the page, I want to have a radio button that is selected redirect me to a page and then submit the form data to the div on that page and that page only, heres the form
<form name = "quoted" method="get">
<input id = "poster" type="text" name="poster" required="required" placeholder = "Credited Individual."> <br>
<textarea class = "actual_quote" name = "actual_quote" required="required" placeholder = "Write the question here!"></textarea><br><br><br>
<div class = "checkboxes" required="required">
<h3 style = "margin-top:-20px;">Please select one catagory that the quote falls into.</h3>
<label for="x"><input type="radio" name="x" value="stupid.php" id = "x" checked="checked" /> <span>stupid</span></label><br>
<label for="x"><input type="radio" name="x" value="stupider.php" id = "x" /> <span>stupider</span> </label><br>
<label for="x"><input type="radio" name="x" value="stupidest.php" id = "x"/> <span>stupidest</span></label>
</div>
<input id = "submit1" type="submit"><br>
</form>
and heres the div where we $_GET the data from the form and post it in the div
<div class="top-submit"><?php echo '“' . (!empty($_GET['actual_quote']) ? $_GET['actual_quote'] : '') . '”'; $actual_quote = $_GET['actual_quote'];?>
</div>
<div class="poster"><?php echo "-" . (!empty($_GET['poster']) ? $_GET['poster'] :''); $poster = $_GET['poster'];?>
<div class = "like">
Like
<p id = "like" style = "color:green;">0</p>
</div>
<div class = "dislike">
Dislike
<p id = "dis" style = "color:red;">0</p>
</div>
</div>
<?php
"INSERT INTO submissions(top-submit, poster)
VALUES ($actual_quote, $poster)";
?>
</div>
</div>
I have been stuck on this issue for a day, and I can't get out, please help!
if it's TL; DR
I want to be able to have one radio option selected, and when the user presses submit I want them to be redirected to the page of the radio option and then i want the data to be posted there and no where else. Please help! Thanks in advance -Connor
You are redirecting but not including the posted data.
It sounds like you should create a hidden form that the users don't see, and when they submit the form you have now, dynamically fill in the needed inputs and then trigger that form, posting the data to the correct page.
See the second answer here: pass post data with window.location.href

Checking Which Button was Clicked

I have a PHP generated form which consists of a list of items, each with a button next to them saying "Remove This" it outputs similar to below:
Item A - [Remove This]
Item B - [Remove This]
...
I wish to be able to click Remove This and it will detect which item it is, and then remove that from the database. Here's my code so far:
selectPlaces.php
<?php
include 'data.php';
mysql_connect($host, $user, $pass) or die ("Wrong Information");
mysql_select_db($db) or die("Wrong Database");
$result = mysql_query("SELECT * FROM reseller_addresses") or die ("Broken Query");
while($row = mysql_fetch_array($result)){
$placeName = stripslashes($row['b_name']);
$placeCode = stripslashes($row['b_code']);
$placeTown = stripslashes($row['b_town']);
$outputPlaces .= "<strong>$letter:</strong> $placeName, $placeTown, $placeCode <input type=\"button\" onclick=\"removePlace()\" value=\"Remove This\" /><br />";
}
mysql_close();
?>
Coupled with my admin.php
<div id="content" style="display:none">
Remove a Place<br><br>
<?php include 'selectPlaces.php'; echo $outputPlaces; ?>
</div>
I know I need to add some javascript to detect which button is clicked but I can't seem to get it working. I tried modifying the onclick="removePlace()" by perhaps passing a variable in the function removePlace(placeID) or something like that, but I'm new to JavaScript and I have no idea how to receive this in the removePlace function.
This seems easier to do without JavaScript. For each entry instead of generating just a button, generate a form that posts to a PHP script that does the deleting.
<form action="deletePlace.php?id=<?php echo $idOfThePlace?>">
<input type="submit" value="Remove This" />
</form>
$idOfThePlace would be the ID with you use to identify the data row.
You don't need JavaScript for that. Try running this example:
<?php var_dump($_POST); ?>
<form action="post.php" method="post">
<p>
<input type="submit" value="a" name="action" />
<input type="submit" value="b" name="action" />
</p>
</form>
You'll see that $_POST['action'] will depend on which button was pressed. For your example, you just need to set the value to identify the item that needs to be deleted. It might be useful to use the <button> element for that: <button name="delete" type="submit" value="12345">delete item 12345</button>. It'll show up as $_POST['delete'] with 12345 as value when submitted.

Categories