I'm trying to create an integer update form field for my cart system. On the page, I already have a + and - function to increase and decrease the quantity by 1. How could I include an integer form field next to the (+) and (-) to udpate by a certain result and then send this to the cart page. Thank you very much for any help.
This is the main part of the code that is enclosed within PHP (I left out the information before the first a href link because it isn't important)
(+)
(-)
I'm not quite sure if i really get your question right, but i think you want to change an input field in a chart.
i think you should solve that problem with JavaScript instead of PHP, because you have to reload the page on every Click.
in jQuery it would like that:
$(".increase").each(function() {
$(this).click(function() {
var ipt = $("#" + $(this).attr("data-what"));
ipt.val(parseInt(ipt.val()) + 1);
});
});
$(".decrease").each(function() {
$(this).click(function() {
var ipt = $("#" + $(this).attr("data-what"));
ipt.val(parseInt(ipt.val()) - 1);
if(parseInt(ipt.val()) < 0) {
ipt.val(0);
}
});
});
and the html is:
<div class="item">Item 1 <input type="text" id="item1" value="1" size="2"> (+1 | -1)</div>
<div class="item">Item 2 <input type="text" id="item2" value="1" size="2"> (+1 | -1)</div>
<div class="item">Item 3 <input type="text" id="item3" value="1" size="2"> (+1 | -1)</div>
see http://jsfiddle.net/axYfq/4/
and with the submit button you get the right quantity :)
You'll have to post a form to cart.php:
<form action="cart.php" method="post">
<input type="text" name="qty"/>
<input type="submit" name="submit_increase" value="(+)"/>
</form>
<form action="cart.php" method="post">
<input type="text" name="qty"/>
<input type="submit" name="submit_remove" value="(-)"/>
</form>
And in your cart.php, you can use if (isset($_POST['submit_increase'])) to determine if you're increasing or decreasing, then $_POST['qty'] to grab the number of items to add/remove.
This isn't the only solution. You could use one form, hidden inputs, or javascript to build a query string. Its up to you.
You could use javascript getelementbyID to do this.
First, you will need a form field with an appropriate ID:
<input type="text" name="quantity" id="NumberOfItems" />
Then put a span around the (+) and (-) to call a JS function:
<span onclick="IncrementNumber()">(+)</span>
<span onclick="DecrementNumber()">(-)</span>
Then you can use getelementbyID to change the value when they click a button:
function IncrementNumber()
{
document.getElementById("NumberOfItems").value-=-1; // Messy, but works
}
function DecrementNumber()
{
document.getElementById("NumberOfItems").value-=1;
}
I believe you need to use a form here. Try doing:
<form action="#" method="POST">
<input type="hidden" name="user_action" value="increase">
<input type="submit" value="increase">
</form>
<form action="#" method="POST">
<input type="hidden" name="user_action" value="decrease">
<input type="submit" value="decrease">
</form>
This way you will output two buttons, and when the user clicks on each one, you will get their action from the $_POST variable. So then you could do something like:
<?php
if(isset($_POST['increase'])){
$number_of_items += 1;
}
if(isset($_POST['decrease'])){
$number_of_items -= 1;
}
?>
<p>You have ordered <?php echo $number_of_items; ?> items</p>
Related
I am still working on my school project, which is almost finished.
With your help, I successfully created a working system that allows users to write, edit and delete data to/from the database.
The only problem I have right now us "user-friendly form." I managed to create auto-focus, insert correct values on edit so the user can see what was previously written in that field, etc.
I have my forms hidden with jquery. When a user clicks add, the form slides in. What I need to achieve is: "when a user clicks submit and the page refreshes and adds the element to the database, the form should appear again so users can add data faster."
Here is my code.
$(document).ready(function() {
$('#x').click(function() {
$('.y').toggle("slide");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="x">Click</div>
<div class="y" style="display: none;">
<div class="container">
<form action="insertzunanja.php" method="POST" id="x">
<input type="hidden" name="narocilo" value="0.1412312">
<input type="hidden" name="id" value="id-1">
<input type="text" name="dolzina" style="width:49%;" placeholder="Dolzina (v cm)">
<input type="text" name="sirina" style="width:49%;" placeholder="Sirina (v cm)">
<input type="text" name="kolicina" value="1" style="width:49%;" placeholder="Kolicina">
<input type="text" name="opombe" style="width:49%;" placeholder="Opombe">
<input type="submit" value="Send">
</form>
</div>
</div>
Thanks and best regards.
You can use AJAX call to send the data to php file instead of form action. According to your code you will have something like this:
<div id="x">Dodaj</div>
<div class="y" style="display: none;">
<div class="container">
<input type="hidden" name="narocilo" value="<?php echo $randomNum; ?>">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="text" name="dolzina" style="width:49%;" placeholder="Dolzina (v cm)">
<input type="text" name="sirina" style="width:49%;" placeholder="sirina (v cm)">
<input type="text" name="kolicina" value="1" style="width:49%;" placeholder="Kolicina">
<input type="text" name="opombe" style="width:49%;" placeholder="Opombe">
<input id="sub" type="submit" value="Send">
</div>
</div>
$(document).ready(function(){
$('#x').click(function() {
$('.y').toggle("slide");
});
});
$("sub").click(function(){
$.post("insertzunanja.php",
{
dolzina: $("input[name=dolzina]"),
sirin: $("input[name=sirina]")
},
function(){
$("input[name=dolzina]").val("");
$("input[name=sirina]").val("");
if($('.y').is( ":hidden" )) {
$('.y').toggle("slide");
}
});
});
Basically, when you click on button you call php with AJAX POST request passing two values dolzina and sirin retrieved by the html code(note: you have more values to pass so change it accordingly) to php file. Jquery deletes the values of the input fields and check if input fields are shown. If not the inputs fields are shown.
If you are using PHP to process the form submission and generate the code in your question, and you always want the form to be displayed after submission, you can do this:
At the PHP code identify submission (e.g. isset($_REQUEST['id']) ).
[if #1 is true] On generating jQuery code, add $('.y').show(); within the ready function (but separated from the existing click function).
Example:
<?php
// your existing code...
?>
$(document).ready(function() {
<?= ( isset($_REQUEST['id']) ? '$(".y").show();' : '' ); ?>
$('#x').click(function() {
$('.y').toggle("slide");
});
});
<?php
// your existing code...
?>
Say I have a HTML form that lets you input a name and age and returns with a list of people with that name and age.
<form method="post" action="/search_results">
<input type="text" name="personName">
<input type="text" name="personAge">
<input type="submit" value="Submit!">
</form>
And on the search results page I have a list of the results, but I only display 50 at a time and allow users to go forwards/backwards between the page with buttons. So the results page would also look for a POSTed 'pageNumber' value and default to 0 if there is none.
When they click a button, how would I resubmit the age and name and also submit the corresponding pageNumber from the button?
I'm using PHP
Add a hidden field to the form:
<form name=search"" method="post" action="/search_results">
<input type="hidden" name="pageNumber"
value="<?php echo isset($_POST['pageNumber']) ? (int) $_POST['pageNumber'] : 0; ?>">
<input type="text" name="personName">
<input type="text" name="personAge">
<input type="submit" value="Submit!">
</form>
Add a JavaScript function to modify the hidden field value:
<script>
function search(pageNumber) {
var form = document.forms.search;
if (!form) return;
form.elements.pageNumber.value = pageNumber;
form.submit();
}
</script>
Apply the JavaScript function for the page buttons:
<span onclick="search(1)">1</span>
<span onclick="search(2)">2</span>
Obviously, the buttons should be generated with PHP in the following manner:
<?php
for ($p = 0; $p < $pagesNum; ++$p) {
echo "<span onclick='search($p)'>$p</span>";
}
?>
<form method="post" action="/search_results">
<input type="text" name="personName" value="<?php echo (isset($formdata['personName'])?$formdata['psersonName']:"") ?>">
<input type="text" name="personAge" value="<?php echo (isset($formdata['personAge'])?$formdata['personAge']:"") ?>">
<input type="hidden" name="currentPage" value="<?php echo (isset($formdata['currentPage'])?$formdata['currentPage']:"0") ?>">
<input type="submit" value="Submit!">
</form>
formdata are the data which are the inputs of the previous submit. These data should be returned by the search_results page along with the view.
Below is the js
<script>
$(document).ready(function(){
$(".paginationBtns").click(function(){
var page = $(this).attr('pageValue');
$("input[name='current']").val(page);
$("form").submit();
});
});
</script>
Assumptions of the forward and backward button
<a href="#" pageValue=0>Back</a><a href="#" pageValue=50>Next</a>
You have to append the back and next pageValue while loading each page.
The best practice in pagination to use a simple link that contains the parameres (GET) and not (POST). That way, when you have the parameters in the url you can cache it, add to favorites, get indexed by google, share your page in email/facebook etc.
<a href='http://example.com/?personName=<?=$_POST['personName']?>&personAge=<?=$_POST['personAge']?>&page=<?=$next_page_number?>'>Next</a>
If for some reason you must or really want to use POST you can save the values in hidden inputs within a form in the page and then sumbit it when clicking on "next page" button.
Form example:
notice its just an example you should sanitize the variables and not put them directly from the $_POST
<form name="pagination" method="post" action="/search_results">
<input type="hidden" name="page" id="page" value="2">
<input type="hidden" name="personName" value='<?=$_POST['personName'];?>'>
<input type="hidden" name="personAge" value='<?=$_POST['personAge'];?>'>
</form>
notice that we set the next page numbers & do submit by using a command from the form of:
<button onclick='document.getElementById("page").value= "2";document.pagination.submit();'>Next page</button>
I've got kind of a situation here : I have the following form :
<form action="sample.php" id="searchform" method="post">
<input type="text" id="key_words" name="key_words" value="<?php echo isset($_POST['key_words']) ? $_POST['key_words'] : '' ?>"style="width:377px;">
<input type="text" name="minimum_price" value="<?php echo isset($_POST['minimum_price']) ? $_POST['minimum_price'] : '' ?>">
<input type="text" name="maximum_price" value="<?php echo isset($_POST['maximum_price']) ? $_POST['maximum_price'] : '' ?>">
I'm using the php script in value because i need to keep the value in text box persistent. So, now i need to clear the values in text box when i click a button:
<button type="reset" value="clear" onclick="clearform()">Clear</button>
I've tried a few things and failed. Help please? JavaScript can also be used for clearform() method.
You just need to get the elements by id and set the value attribute to empty string:
function clearform()
{
document.getElementById('key_words').value = '';
//same thing for other ids
}
For your minimum_price and maximum_price you need to add an id since you only have name right now.
Also in this case since you don't want to use HTML's standard reset functionality don't make the button type reset.
It`s will be work for you:
<form action="sample.php" id="searchform" method="post">
<input type="text" id="key_words" name="key_words" value="test1"style="width:377px;">
<input type="text" name="minimum_price" value="test2">
<input type="text" name="maximum_price" value="test3">
<button type="reset" value="clear" onclick="clearform()">Clear</button>
</form>
<script>
function clearform() {
var form = document.getElementById("searchform");
var textFields = form.getElementsByTagName('input');
for(var i = 0; i < textFields.length; i++) {
textFields[i].setAttribute('value', '');
}
}
</script>
I've a page with two formulas one. Each one has it's own form with inputs etc. They both call the same Javascript function on key up.
Only the first one works, I kind of understand why but I can't find a resolution, I'm too new to Javascript to know how to tackle the problem. I can't change the structure of the JS file a great deal as other equations on other pages depend on this set up.
Is there a workaround?
Shortened HTML:
<div id="formula">
<p>To find ρ<sub>b</sub>:</p>
<form id="formula" name="formula">
<input type="hidden" id="formulaName" name="formulaName" value="porosityRhob"/>
<div>
<label>$\rho_{fl}$:</label>
<input type ="text" name="input" id="input" onkeyup="calculatePEFormula()"/>
</div>
<div>
<label>Result:</label>
<input type="text" id="result" name="result">
</div>
</form>
</div>
<br/>
<div id="formula">
<p>To find Φ:</p>
<form id="formula" name="formula">
<input type="hidden" id="formulaName" name="formulaName" value="porosityPhi"/>
<div>
<label>$\rho_{ma}$:</label>
<input type ="text" name="input" id="input" onkeyup="calculatePEFormula()"/>
</div>
<div>
<label>Result:</label>
<input type="text" id="result" name="result">
</div>
</form>
</div>
THE JS:
function PEFormula(result, formulaName){
this.result = result;
this.formulaName = formulaName;
}
function calculatePEFormula(){
var PEObject = new PEFormula($("#result"), $("#formulaName"));
var formulaName = $("#formulaName").val();
switch(formulaName){
case "porosityRhob" : PEObject.porosityRhoB();
break;
case "porosityPhi" : PEObject.porosityPhi();
break;
}
PEFormula.prototype.porosityPhi = function(){
var input = parseFloat($("#input").val());
//logic
return r;
}
The HTML attribute id is supposed to be unique
<div id="formula">
<form id="formula" name="formula">
<input type="hidden" id="formulaName" name="formulaName" value="porosityRhob"/>
<input type ="text" name="input" id="input" onkeyup="calculatePEFormula()"/>
<input type="text" id="result" name="result">
Try changing these ids instead use classes
Here is the solution change all field ids to class and form id should be different and pass parameter id of form as parameter onkeyup="calculatePEFormula('form#formula1')" and onkeyup="calculatePEFormula('form#formula2')" Now in js
function PEFormula(result, formulaName){
this.result = result;
this.formulaName = formulaName;
}
function calculatePEFormula(form_id){
var PEObject = new PEFormula($(form_id+" .result"), $(form_id+" .formulaName"));
var formulaName = $(form_id+" .formulaName").val();
switch(formulaName){
case "porosityRhob" : PEObject.porosityRhoB();
break;
case "porosityPhi" : PEObject.porosityPhi();
break;
}
PEFormula.prototype.porosityPhi = function(){
var input = parseFloat($(form_id+" .input").val()); //provide the form id here
//logic
return r;
}
You're using ID's when you should be using classes.
You need to avoid assigning the same id attribute to multiple elements. An element's ID should be unique.
Never use the same id for multiple elements. That's a basic concept in HTML. An identification should be unique and identical. So there cannot be any duplicated IDs inside any HTML page. So please change the ids of 2nd form,
<div id="formula2">
<p>To find Φ:</p>
<form id="formula2" name="formula">
<input type="hidden" id="formulaName2" name="formulaName" value="porosityPhi"/>
<div>
<label>$\rho_{ma}$:</label>
<input type ="text" name="input" id="input2" onkeyup="calculatePEFormula()"/>
</div>
<div>
<label>Result:</label>
<input type="text" id="result2" name="result">
</div>
</form>
</div>
Change the IDs accordingly when calling the same js function.
I have renamed all the IDs by adding 2 at the end of each ID. Make every ID unique.
This form should calculate numbers and save
Now there are two buttons One is call Calculator and two call Save
If I press Calculator
I get the form action is going to file name save.php And I do not want it that way
How can I set it up that button do something else
Example
Calculator = Calculator
Save = save.php
Is it possible to set it
Because it is one form
Thanks to anyone who can help
<?php
error_reporting (0);
$NUM = $_POST["NUM"];
$NUM2 = $_POST["NUM2"];
$NUM = "$NUM";
$NUM2 = "$NUM2";
$subtotal= $NUM+$NUM2;
?>
<form action="save.php" method="POST" name="Calculator">
<p>
<input name="NUM" type="text" value="<?php echo $_POST["NUM"]; ?>" />
</p>
<p>+</p>
<p>
<input name="NUM2" type="text" value="<?php echo $_POST["NUM2"]; ?>" />
</p>
<p>
<input name="subtotal" type="text" value="<?php echo "$subtotal";?>" />
</p>
<p>
<input name="submit" type="submit" value="Calculator" />
<p>
<input name="submit" type="submit" value="Save" />
</p>
</form>
You can have all the logic in a single PHP script (no need to direct to a different script depending on the button). If the logic is complicated, use include statements in order to separate the code.
Name the buttons differently:
<input name="calculator_submit" type="submit" value="Calculator" />
<input name="save_submit" type="submit" value="Save" />
Then in PHP:
if (isset($_GET['calculator_submit'])) {
// ...
} else if (isset($_GET['save_submit'])) {
// ...
} else {
// ...
}
If you really need different PHP script, then you'll have to go with Javascript (function will change the form action when a submit is clicked).
Since you are now using two submit buttons, both will submit the form and go to save.php.
Make your "calculator" button an input type=button instead of submit, and handle it via JavaScript.
Just FYI:
HTML5 allows to define a different form target URL by specifying the formaction attribut on a submit button – but browser support is lousy as of now.
Form and Buttons
<input name="submit" type="button" onclick="submitForm('Calculator')" value="Calculator" />
<input name="submit" type="button" onclick="submitForm('Save.php')" value="Save" />
Some jquery:
function submitForm(path) {
$('#Calculator').attr('action', path);
$('#Calculator').submit();
}