can you help for quantity input box on featured products , opencart 2.3.0.2
<input type="text" size="3" id="quantity-<?php echo $product['product_id']; ?>" name="quantity" value="1" placeholder="1" class="form-control my-form-control input-lg qty-input radius-none pull-left" />
this code will show input box and default 1 and it adds 1 products to cart but I need any numbers to be added to cart.
You should change the value attribute. Like value="<?php echo $needed_quantity; ?>"
Or delete value attribute but than you need to check if user set the quantity (check if empty) and inform user that he must set the quantity manually
Related
So, I am trying to get the quantity of the user input and if it matches the total quantity stock then it will be sold as a wholesale product. But I cannot get the jquery function add_cart_func('qty') working. Please help!! Trying for hours.
Edit- I have added the declaration of let qty = $("#"+type).val();. I have used radio for users to choose if they want to buy wholesale or retail.
<?php
if ($this->session->user=='good')
{
$price = $single['pro_quantity_stock'] == add_cart_func('qty') ? $single["pro_wholesale"] : $single["pro_retail"];
?>
<input type="radio" name="tt" value="Wholesale" onclick="doDisplay(this);"/> Buy Wholesale
<span id="wholesales" style="display:none">
<?php
if ($single['pro_wholesale'] != NULL)
{
?>
<input name="qty" class ='txtbx' size="10" type="number" id="txtNumber" value="<?php echo $single['pro_quantity_stock'] ?>" disabled />
<?php
}
else
{
?>
<input name="qty" class ='txtbx' size="10" type="number" id="txtNumber" value="0" disabled />
<?php
}
?>
</span>
<br />
<input type="radio" name="tt" value="Retail" onclick="doDisplay(this);"/> Buy Retail
<span id="retails" style="display:none">
<input name="qty" class ='txtbx1' size="10" type="number" id="txtNumber1" />
</span>
<br />
<a href="#" class="btn btn-info btn-normal" onclick="add_cart_func(<?= $single['id'] ?>, '<?= $single["pro_title"] ?>', '<?= $price ?>')">
<span class="glyphicon glyphicon-shopping-cart"></span> ADD TO CART
</a>
There's a serious misunderstanding on your question. The one being that you're trying to use PHP in a real-time client logic. Here're some important things to note:
PHP is server side and Javascript/Jquery are client side. So the php on your pages is executed before the pages are returned to the browser. AS soon as the pages reach the browser no php runs at that point, you're only left with client site javascript.
There's a function add_cart_func() which is not clear if it's declared both as a php function and javascript function before you did not provide their declarations. So we cannot really assume that is the case but it seems like you're trying to use either a php function as a javascript function like here onclick="add_cart_func(<?= $single['id'] ?> or perhaps it's a javascript function and you're trying to use it within your php code.
PHP and Javascript are not interchangeable as per bullet 1, so to be assisted you will need to provide the declarations of
I am trying to displaying the input fields depending upon the what outputs are coming from the database. Please share any idea or logic in this. I have more than 25 fields.
Page1.php
I have Name(Text type), Email(Email type), gender(Radio), country(Select dropdown), Address( Textarea) in the form. The user will click on check box whatever he needs from the form and click on submit then the value of the fields which he selects will store in the database.
Form page
Page2.php
Here I am displaying the fields which are selected from Page1.php but also I need input type related to which fields come from the database.
For example: In the page1 I choose Name, Email, Country and submitted the form then In page2 I have to display the output <input type="text" name="Name">,<input type="text" name="Name">,<select></select>.
I need to know what logic I have to use it. Can any one help me with the script?
Form code
<form action="" method="post">
<input type="checkbox" name="check-fields[]" value="Name">
<label>Name(Text type)</label>
<input type="checkbox" name="check-fields[]" value="Email">
<label>Email(Email type)</label>
<input type="checkbox" name="check-fields[]" value="Gender">
<label>Gender(Radio type)</label>
<input type="checkbox" name="check-fields[]" value="Select">
<label>Country(Drop down)</label>
<input type="checkbox" name="check-fields[]" value="textarea">
<label>Address(Textare)</label>
<input type="submit" name="submit" value="submit">
</form>
Storing the value in the database
if (isset($_POST['submit'])) {
// prepare and bind
$a=$_POST['check-fields'];
$elements= implode(',',$a);
$sql_elements="INSERT INTO test (elements) VALUES (?)";
$stmt = $conn->prepare($sql_elements);
$stmt->bind_param("s", $elements);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
}
Page 2
I am getting output but how can I set the input fields?
$sql_fields="SELECT elements FROM test WHERE id=3";
if ($stmt = $conn->prepare($sql_fields)) {
$stmt->execute();
$stmt->bind_result($elements);
while ($stmt->fetch()) {
$arr=explode(",", $elements);
echo "<pre>";
print_r($arr);
echo "</pre>";
}
}
$stmt->close();
$conn->close();
?>
You're trying to create a personalized form for different users if I'm correct, meaning that you will allow a user to generate a form based on a selection of fields the user can choose from.
What you need to do is create a table called "forminputtypes" where you create a row for each different input field you can think of, and want to give the user as a choice to select from. Your table would looke like this:
Id | FieldName | Type | etc ->
-------------------------------
1 | Name | text | ...
2 | Email | email | ...
3 | Gender | radio | ...
You can add more columns in the table to store more information (think of possible values in a radio input or a dropdown).
Now at in Page1.php you select all input types from this table, and display them like you already are. However, the value of these checkboxes will be the corresponding Id value of the record in the database like so:
<input type="checkbox" name="check-fields[]" value="3">
Now when someone chooses the Gender field, you can see in your Page2.php that the user did so by matching his choice '3' to the record in the database. If you want to save this information, you can create another table called UserFormInputFields which will function as a couple table between your user table and the FormInputTypes table.
When you want to display the form in Page2.php, you simply get all the input fields the user chose by selecting on Id from the FormInputTypes table. Since you know the type of each input field (because it's a column in the table) you can display them all correctly.
If you want to do it in core PHP only.Just put if else around the html tags.If value is coming from Database then you show particular tag else nothing. You can modify your page 2 code like.
<?php
$sql_fields="SELECT elements FROM test WHERE id=3";
if ($stmt = $conn->prepare($sql_fields)) {
$stmt->execute();
$stmt->bind_result($elements);
while ($stmt->fetch()) {
$arr=explode(",", $elements);
echo "<pre>";
print_r($arr);
echo "</pre>";
}
}
$stmt->close();
$conn->close();
?>
<form action="" method="post">
<?php if(in_array("Name",$arr)) { ?>
<input type="text" name="name" >
<label>Name</label>
<?php } ?>
<?php if(in_array("Email",$arr)) { ?>
<input type="email" name="email">
<label>Email</label>
<?php } ?>
<?php if(in_array("Gender",$arr)) { ?>
<input type="radio" name="gender" value="Male">
<input type="radio" name="geneer" value="Female">
<label>Gender(Radio type)</label>
<?php } ?>
//.............write other fiels like this.
<input type="submit" name="submit" value="submit">
</form>
I hope this helped.
How can I make a form like posting my ingredient but in the way that I will only edit the posted ingredients
this is the Javascript of the button
<script>
$(document).ready(function() {
var max_fields = 25; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e) { //on add input button click
e.preventDefault();
if (x < max_fields) { //max input box allowed
x++; //text box increment
$(wrapper).append('<div class="col-md-6 div-' + x + '"><input type="text" class="form-control" name="mytext[]" required/></div>'); //add input box
$(wrapper).append('<div class="col-md-6 div-' + x + '"><input type="text" class="form-control" name="mytext2[]" required/>Remove</div>');
}
});
$(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
e.preventDefault();
$(".div-" + $(this).attr("div-id")).remove();
x--;
})
});
</script>
This is the form of inserting the ingredients
<div class="input_fields_wrap">
<div class="col-md-6">
<h5>Notes/Scaling (quantity, additional info)</h5>
<input type="text" class="form-control" name="mytext2[]" required>
<br>
</div>
<div class="col-md-6">
<h5>Name of Ingredient:</h5>
<input type="text" class="form-control" name="mytext[]" required>
<br>
</div>
<br>
</div>
<div class="col-md-12">
<button class="add_field_button button loading-pulse">Add More Fields</button>
</div>
this is how i implode it
$ingredient_name =implode("^",$_POST["mytext"]);
$ingredient_value = implode("^", $_POST["mytext2"]);
this is how i explode it
$ingredient_name = explode("^",$ingredient_name);
$ingredient_value = explode("^", $ingredient_scale);
$ingredients = array_combine($ingredient_value,$ingredient_name);
this is how i loop it
<table class="table-bordered" style="width:90%;">
<th style="text-align:center;">Ingredient Scale</th>
<th style="text-align:center;">Ingredient Name</th>
<?php
foreach($ingredients as $amount => $name)
{
?>
<tr>
<td style="margin:left:10px;"><?php echo "{$amount}"?></td>
<td style="margin:left:10px;"><?php echo "{$name}"?></td>
</tr>
<?php
}
?>
</table>
for example in the database
there are 2 columns,
recipe_ingredient_value recipe_ingredient_name
1/4,1/2,1 cup sugar,meat,water
i want it to extract into an input fields that i can edit (rename, add ,delete)
for example
1/4 sugar remove
1/2 meat remove
1 cup water remove
add more fields
You should print all values in a form inside the loop and then you need some sort of "Save" button and a route ready to receive new values. This route should be a php script, for example, that would loop through your new data and perform an update.
I'm suggesting you're writing PHP framework-free, standalone app. That way you have to think about a lot of things, which is good for you if you are a beginner.
Now back to the point:
It would be great if you could print a hidden id of a database row, somewhere inside the loop:
<tr>
<td><input type="text" name="recipe_id" value="<?php echo $amount; ?> /></td>
<td><input type="text" name="recipe_id" value="<?php echo $name; ?> /></td>
<input type="hidden" name="recipe_id" value="<?php echo $recipe_id; ?> />
</tr>
Remember that route that collects and updates the data? Let's call it "update-script.php".
The update-script.php should perform a check for every field in a $_GET or $_POST variable, to see if that value has been changed. If yes, the row should be updated, if not, then it should be skipped. It's up to you to create the logic for this.
Furthermore, I noticed that your javascript code needs some adjustments. I will only hint one thing that kinda sticks out:
$(add_button).click(function(e){
For the sake of your future work, please use "on" method that is described here:
http://api.jquery.com/on/
Trust me, when you start working with DOM elements, changing the DOM and adding new events, this will be your savior.
Good luck!
I think the easiest way would be to structure your inputs like this.
<tr>
<td>
<input type="hidden" name="recipe[<?= $recipe_id; ?>][id]" value="<?php echo $recipe_id; ?>" placeholder="value"/>
<input type="text" name="recipe[<?= $recipe_id; ?>][value]" value="<?php echo $value; ?>" placeholder="name"/>
</td>
<td><input type="text" name="recipe[<?= $recipe_id; ?>][name]" value="<?php echo $name; ?>" /></td>
<td><input id="recipe_<?= $recipe_id; ?>" type="checkbox" name="recipe[<?= $recipe_id; ?>][delete]" value="1" /><label for="recipe_?= $recipe_id; ?>">Delete</label></td>
</tr>
Where $recipe id is the row id. Then your javascript would add more rows like
<tr>
<td><input type="text" name="recipe[2][value]" value="" placeholder="value" /></td>
<td><input type="text" name="recipe[2][name]" value="" placeholder="name" /></td>
<td></td>
</tr>
Then in your php you can see the array will be formatted nicely into arrays of recipes like:
array(
array(
'name' => 'Pie'
'value' => 2
'recipe_id' => 1
),
array(
'name' => 'Deleted Pie'
'value' => 2
'recipe_id' => 2
'deleted' => 1
),
array(
'name' => 'New pie'
'value' => 2
),
)
Then using the deleted key you can check if you need to delete the Database row and if it has the recipe_id key then it might need to be updated. If it doesn't have the recipe key then its a new recipe.
I have a problem regarding obtaining id from Combo Box populating name of customer from a database containing field id & name. I want to get the id of name selected by user from Combo Box. Following is the code i have applied to populating Combo Box:
<input type="text" class="input-field" id="registerNumber" name="register_number"
list="register_number" placeholder="Select Register No" /></label>
<!-- Populating Editable ComboBox -->
<datalist id="register_number" >
<?php
$result_set= mysqli_query( $connection, "Select id, customer_name from
service_register ");
while($result = mysqli_fetch_assoc($result_set)){
echo "<option value=\"{$result["customer_name "]}\"
id=\"{$result["id"]}\"></option>";
}
?>
You dont have to set the type of the input to text and you should wrap the input with a form tag. The form tag will have the url of the server side code that will handle the form submission. A submit button should be provided for the user. For clarity you should visit this example http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_datalist
<form action="server_code.php" method="get">
<input class="input-field" id="registerNumber" name="register_number"
list="register_number" placeholder="Select Register No" /></label>
<!-- Populating Editable ComboBox -->
<datalist id="register_number" >
<?php
$result_set= mysqli_query( $connection, "Select id, customer_name from
service_register ");
while($result = mysqli_fetch_assoc($result_set)){
echo "<option value=\"{$result["customer_name "]}\"
id=\"{$result["id"]}\"></option>";
}
?>
<input type="submit">
</form>
First of all, I'm sorry if this question has already been asked and solved before, but I cant find the exact answer/solution for my problem.
My question is, how do I calculate grand-total if a user changes quantity and/or selects more that 1 product? My product item list and value is from mysql.
Here's my form details:
<?php $price = 10.00;?>
<input name="qty_<?php echo $obj->product_code;?>" type="text" id="qty<?php echo $obj->product_code;?>" value="0" size="2" onkeyup="calculate()">
<input name="subtotal_<?php echo $obj->product_code;?>" type="text" id="subtotal_<?php echo $obj->product_code;?>" value="0.00" size="7" readonly>
<input name="qty_<?php echo $obj->product_code;?>" type="text" id="qty<?php echo $obj->product_code;?>" value="0" size="2" onkeyup="calculate()">
<input name="subtotal_<?php echo $obj->product_code;?>" type="text" id="subtotal_<?php echo $obj->product_code;?>" value="0.00" size="7" readonly>
<input name="grandtotal" type="text" id="grandtotal" value="0.00" readonly>
I'm using my php/mysql results value for my input field name/id, as it's easier for me to identify/pass the value when submitted.
Here's my Javascript:
function calculate()
{
var quantity = document.getElementById('<?php echo $obj->product_code;?>').value;
var currprice = <?php echo $obj->product_code.$obj->product_price;?>;
var total = quantity * currprice;
document.getElementById('subtotal_<?php echo $obj->product_code;?>').value = total.formatMoney(2,',','.');
}
So, how do I calculate the subtotal from each product and display it at grand total text field?
I've already searched Google but have not been able to solve my problem.
1) Using the right ID's
The id's of the quantity fields do not match with the getElementById in the function. It should start with "qty_", not directly the product code. Also, for the sake of regularity, change the id's in the <input>'s from "qty" to "qty_".
2) The right structure
The html you use now, has double quantity and subtotal <input> fields with the same ID. This causes Javascript to be unable to access these values. Make sure you distinguish between the names and ID's of these input fields.
PHP:
$products = array(
array("price"=>10, "code"=>"product1"),
array("price"=>25, "code"=>"product2")
);
foreach($products as $key => $productInfo) {
/* echo a hidden field with the price per product (for calculation later) */
echo '<input type="hidden" name="price_'. $productInfo["code"] .'" id="price_'. $productInfo["code"] .'" value="'. $productInfo["price"] .'" />';
/* echo the quantity field for this product */
echo '<input class="qty" name="'. $productInfo["code"] .'" id="'. $productInfo["code"] .'" type="text" value="0" size="2" onkeyup="calculate()">';
/* echo the subtotal field for this product */
echo '<input name="sub_'. $productInfo["code"] .'" type="text" id="sub_'. $productInfo["code"] .'" value="0.00" size="7" readonly>';
}
/* echo the field for total price */
echo '<input type="text" name="total" id="total" value="0.00" />';
This results in sets of three input fields per product:
One for the price per product (id="price_THEPRODUCTCODE")
One for the quantity (id="THEPRODUCTCODE")
And one for the subtotal (id="sub_THEPRODUCTCODE")
Javascript
I suggest using the jQuery library for faster and easier collection of the data from the input fields here.
The function would then be:
function calculate() {
var total = 0;
// for each quantity field:
$("input.qty").each(function() {
// get product code
var productCode = $(this).attr("id");
// get price per product
var price = $("#price_"+productCode).val();
// get quantity
var quantity = $(this).val();
// calculate subtotal
var subtotal = (price*quantity);
// put new subtotal back in the input field
$("#sub_"+productCode).val(subtotal);
// add subtotal to total
total += subtotal;
});
// put total in the input field of total
$("#total").val(total);
}