How to get values of selected dropdown box display in url - php

I'm setting up an eCommerce website and I need to select product quantity user enters in drop down. I have tried this but it gives me last quantity not the selected. I'm still in the process of creating the addtocart.php file but before that I need to pass the values so that I can use them in a later stage.
I have implemented the following code
<div class="product-cartq">
<div class="product-quantity pull-left">
<span>Quantity:</span>
<select name="quantity_option">
<?php for($qty = 1; $qty < 11; $qty++){
echo '<option value="'.$qty.'">'.$qty.'</option>';
}?>
</select>
</div>
<div class="btn_addtocart">
<form method="GET" action="cart.php" enctype="multipart/form-data">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="quantity_option" value="<?php echo $qty; ?>">
<input class="btn btn-primary btn-submit" type="submit" name="add_to_cart" value="add to cart"/>
</form>
</div>
</div>
I can't proceed as I'm unable to get the value. Any suggestions or improvements on this?

Do this:
<form method="GET" action="cart.php" enctype="multipart/form-data">
<div class="product-cartq">
<div class="product-quantity pull-left">
<span>Quantity:</span>
<select name="quantity_option">
<?php for($qty = 1; $qty < 11; $qty++){
echo '<option value="'.$qty.'">'.$qty.'</option>';
}?>
</select>
</div>
<div class="btn_addtocart">
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>">
<input type="hidden" name="quantity_option" value="<?php echo $qty; ?>">
<input class="btn btn-primary btn-submit" type="submit" name="add_to_cart" value="add to cart"/>
</div>
</div>
</form>

The select control is outside form tag.

Related

Cannot display edit form when clicked on edit button and the form doesn't parse error and displays blank page

EDIT: My edit button refuse to work.
I'm trying to edit this page, but it seems to be breaking when I click the edit button. it is retrieving data from the database and displaying on faculty.php successfully while if I try to edit it shows a blank page. what is wrong with my code, please.
faculty.php: a separate page where i have my edit button
<td>
<form action="facultyedit.php" method="POST">
<input type="hidden" name="edit_id" value="<?php echo $row['id']; ?>" >
<button type="submit" name="edit_data_btn" class="btn btn-success">EDIT</button>
</form>
</td>
facultyedit page: where i receive the edit button
<?php
if (isset($_POST['edit_data_btn']))
{
$id = $_POST['edit_id'];
$query = " SELECT * FROM register WHERE id='$id' ";
$query_run = mysqli_query($connection, $query);
foreach ($query_run as $row ) {
?>
<form action="" method="POST" >
<input type="text" name="edit_id" value="<?php echo $row['id'] ?>" >
<div class="form-group">
<label>Name:</label>
<input type="text" name="edit_name" value="<?php echo $row['name']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Designation:</label>
<input type="text" name="edit_designation" value="<?php echo $row['design']; ?>" class="form-control" >
</div>
<div>
<label>Description:</label>
<input type="text" name="edit_description" value="<?php echo $row['descrip']; ?>" class="form-control" >
</div>
<div class="form-group">
<label>Upload Image:</label>
<input type="file" name="edit_faculty_image" id="faculty_image" value="<?php echo $row['image']; ?>" class="form-control" >
</div>
<div class="">
Cancel
<button type="update" name="update_aboutusbtn" class="btn btn-primary">Update</button>
</div>
</form>
<?php
}
}
?
I have fixed the issue:
The problem was that I was selecting a register table from the database instead of the faculty table.

Magento 2: Add to cart button in category page with pre-selected attributes of configurable child

I want to create an add to cart button for a child of a configurable product in the category page.
I'm trying with this code where I add the super_attribute hidden fields with the attributes values.
<?php $postParams = $block->getAddToCartPostParams($_product); ?>
<form data-role="tocart-form" action="<?php echo $postParams['action']; ?>" method="post">
<input type="hidden" name="product" value="<?php echo $_product->getId(); ?>">
<input type="hidden" name="<?php echo Action::PARAM_NAME_URL_ENCODED; ?>" value="<?php echo $postParams['data'][Action::PARAM_NAME_URL_ENCODED]; ?>">
<?php foreach ($attributeOptions as $attr){ ?>
<input type="hidden" name="super_attribute[<?php echo $_child->getResource()->getAttribute($attr)->getData("attribute_id")?>]" value="<?php echo $super_attr[$attr] ?>">
<?php } ?>
<?php echo $block->getBlockHtml('formkey')?>
<div class="fieldset">
<div class="actions">
<button type="submit" title="<?php echo $block->escapeHtml(__('Add to Cart')); ?>" class="action tocart primary">
<span><?php echo __('Add to Cart') ?></span>
</button>
</div>
</div>
</form>
The result is (for example)
<form data-role="tocart-form" action="baseurl/product-name?options=cart" method="post">
<input type="hidden" name="product" value="5858">
<input type="hidden" name="uenc" value="abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd">
<input type="hidden" name="super_attribute[185]" value="10">
<input type="hidden" name="super_attribute[186]" value="12">
<input name="form_key" type="hidden" value="DDAAkjkkkkkkj">
<div class="fieldset">
<div class="actions">
<button type="submit" title="Add to Cart" class="action tocart primary">
<span>Add to Cart</span>
</button>
</div>
</div>
</form>
But when I try to add to cart, I'm redirected to the product page with the error "You need to choose options for your item."
I'm working on the v2.3.1.
Do you have suggestions on how to specify the options for the add to cart in the add to cart button? Thanks!

Passing variable value to URL

I want to pass a variable value to an url but the problem is when I first clicked the search button the value doesn't appear and when the second clicked is triggered the variable value is shown. How can i fix this? I think the fault is when i click the search button the page is refreshing. Can someone help me about this?
here's my code:
<div class="form-inline form-padding">
<form action="student.php?classid=<?php echo $classid;?>" method="post">
<input type="text" class="form-control" name="search" placeholder="Search by ID or Name">
<select name="subject" class="form-control" required>
<option value="">Select Subject...</option>
<?php while($row = mysql_fetch_array($mysubject)): ?>
<option value="<?php echo $row['id']?>" <?php if($row['id']==$classid) echo 'selected'; ?>><?php echo $row['subject'];?></option>
<?php endwhile; ?>
</select>
<button type="submit" name="submit" id="submit" class="btn btn-primary"><i class="fa fa-search"></i> Search</button>
<button type="button" name="submit" class="btn btn-success"><i class="fa fa-print"></i> Print</button>
</form>
</div>
in your
<form action="student.php?classid=<?php echo $classid;?>" method="post">
change it to
<form action="student.php" method="post">

PHP to reload page from modal

i am trying to reload page using onClick="location.href='Edit_Equip.php?txtidforsite='" /> but i get Notice: Undefined variable: id
i have tried to define id but still get errors any help would be great
<?php
include('connect.php');
if(isset($_POST['txtidforsite']))
{
$id=$_POST['txtidforsite'];
if(isset($_POST['submit']))
{
$query3 = mysql_query("UPDATE ftn_site_equip SET siteidequip='".$_POST['txtidforsite']."', patype='".$_POST['txtpa']."', drxtype='".$_POST['txtdrx']."' WHERE siteidequip='$id'");
if($query3)
{
header('refresh:2; url=#openModa3');
}
}
$query1=mysql_query("select * from ftn_site_equip where siteidequip='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="" align="center">
<input name="txtidforsite" value="<?php echo $query2['siteidequip']; ?>" size="36" class="siteinfotable"/>
<input name="txtdrx" value="<?php echo $query2['drxtype']; ?>" size="36" class="siteinfotable"/>
<input name="txtpa" value="<?php echo $query2['patype']; ?>" size="36" class="siteinfotable"/>
<input type="submit" name="submit" class="update_record_button" value="Update Record" onclick="window.location='#openModal2';" />
</form>
<?php
}
?>
<div id="openModal" class="cancel_modal">
<div align="center">
<h2 align="center">Are you sure you want to cancel all changes ?</h2>
<input name="yes" type="button" value="Yes" class="yes_button" onClick="location.href='Edit_Form.php?txtid='" />
<input name="no" type="button" value="No" class="no_button" onclick="location.href='#close'"/>
</div>
</div>
<div id="openModal2" class="cancel_modal">
<div align="center">
<img alt="" src="Images/tick.jpg"/><br/>
<h2 align="center">Record Successfully Updated</h2>
</div>
</div>
<div id="openModa3" class="cancel_modal">
<div align="center">
<h2 align="center">What Next ?</h2>
<input name="yes" type="button" value="Retrun to Site Information" class="yes_button" onClick="location.href='Edit_Equip.php?txtidforsite=<?php echo $id; ?>'" />
<input name="no" type="button" value="Search for Another Site" class="no_button" onclick="location.href='search.php'"/>
</div>
</div>

cannot save/retrieve value using html input in a foreach with php

For some probably obvious to anyone else reason, I cannot save the input value of my radio buttons and retrieve them with php. $_POST['answerToQuestion'] is not empty and will print the $key of each, but the value is empty. Can anyone readily see my mistake?
html:
<form action="answerQuestion.php" method="post">
<?php foreach($questions as $k => $q):
if(!$q['is_subquestion']):?>
<div class="questionAnswer">
<?php echo $q['body']; ?><br/>
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" class="btn" id="answer_yes" name="answerToQuestion[<?php echo $k; ?>]" value= 1>Yes</button>
<button type="button" class="btn" id="answer_no" name="answerToQuestion[<?php echo $k; ?>]" value= 0>No</button>
<button type="button" class="btn" id="answer_na" name="answerToQuestion[<?php echo $k; ?>]" value= 2>N/A</button>
</div>
<input type="hidden" id="hidden_2" name="answerToQuestion[<?php echo $k; ?>]" value="">
</div>
<?php endif;?>
<?php endforeach; ?>
<input type="submit" value="Next" name="submit-form" />
</form>
php:
foreach($_POST['answerToQuestion'] as $key=>$value)
{
echo ' '.$value.'<br/>';
}
Basing this answer on the question which clearly states "my radio buttons".
You don't need to echo the $k variable and you should use actual radio buttons:
<form action="answerQuestion.php" method="post">
<div class="btn-group" data-toggle="buttons-radio">
<label><input type="radio" name="answerToQuestion" value="1"> Yes</label>
<label><input type="radio" name="answerToQuestion" value="0"> No</label>
<label><input type="radio" name="answerToQuestion" value="2"> N/A</label>
</div>
<input type="submit" value="Submit">
</form>
Also, remove the spaces value= 0 (from your example), I use quotes as well.
$_POST['answerToQuestion'];
That should work.

Categories