i have a little problem about PHP
there is two input for num1 and num2
and another input answer,,can the output in the php be putted in the input text answer??
<input type="text" name ="num1">
<input type="text" name ="num2">
<input type="text" name ="answer">
Try something like this:
<?php
if(!empty($_POST['num1'])){
$num1 = $_POST['num1'];
$num2 = $_POST['num2']
$answer = $num1 + $num2;
}
?>
<form action="yourpage.php" method="post">
<input type="text" name ="num1" value="<?=#$num1?>">
<input type="text" name ="num2" value="<?=#$num2?>">
<input type="text" name ="answer" value="<?=#$answer?>">
<br/>
<input type="submit" id="btnSubmit" value="POST ME" />
</form>
Not tested btw...
<?php
//get the values and calc..
$answer = $_GET['num1'] + $_GET['num2'];
//more secure, $answer = floor($_GET['num1']) + ...
?>
<html><body>
<form method="get" action=".">
<input type="text" name ="num1">
<input type="text" name ="num2">
<input type="text" name ="answer" value="<?php echo $answer;?>">
<input type="submit">
</form>
...
The jQuery answer that was suggested by riky has shown up on SO before. Try:
How to update the value in one text box based on the value entered in another text box?
As per the OP's request for an fleshed out version of the jQuery answer, modifying the linked to answer gives something like:
$('#num1').change(function() {
var txtAmtval = $('#num1').val() + $('#num2').val();
$('#answer').val(txtAmtval);
});
$('#num2').change(function() {
var txtAmtval = $('#num1').val() + $('#num2').val();
$('#answer').val(txtAmtval);
});
Is that what you had in mind?
Obviously you'd need to include jQuery too.
Please see code in action here: http://jsfiddle.net/9KEsY/
Related
I dont know what im doing wrong. But i get this
Notice: Undefined variable: num1 in D:\Programs\XAMPP\htdocs\homework\addsub.php on line 15`
<?php
if(isset($_POST['sub']))
{
$num1=$_POST['t1'];
$num2=$_POST['t2'];
if ($_POST['sub']=="+") {
$res= $num1 + $num2;
}
elseif($_POST['sub']=="-"){
$res = $num1-$num2;
}
}
?>
<form action="addsub.php" method="POST">
<input type="text" name="t1" value="<?php echo $num1;?>"><br>
<input type="text" name="t2" value="<?php echo $num2;?>"><br>
<input type="text" name="res" value="<?php echo $res;?>"><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
</form>
When I use $num1 or $num2 in textbox values, it shows error. One of my friends used this same code on his laptop but he is using much older version of Xampp. It works fine but later versions of Xampp gives this error. I am using Xampp v3.2.1.
Or initialize the variable if it doesn't exist, like so:
<?php
if (!isset($num1)) {
$num1 = '';
}
Then your HTML could remain unchanged.
The reason I recommend this approach is that it creates clean code - the HTML will always display the value of $num1 and if you choose to initialize it to a different value later, it should be easier to find in the PHP.
use isset to check variable exist or not.
example
<input type="text" name="t1" value="<?php echo isset($num1)?$num1:""; ?>"><br>
You set the $num1 variable only when,
if(isset($_POST['sub']))
So, if the $_POST['sub'] is not there the variable is undefined!
Here is the code, which may help you:
<?php
var $res="";
var $num1="";
var $num2="";
if(isset($_POST['sub']))
{
$num1=$_POST['t1'];
$num2=$_POST['t2'];
if ($_POST['sub']=="+") {
$res= $num1 + $num2;
}
elseif($_POST['sub']=="-"){
$res = $num1-$num2;
}
}
?>
<form action="addsub.php" method="POST">
<input type="text" name="t1" value="<?php echo $num1;?>"><br>
<input type="text" name="t2" value="<?php echo $num2;?>"><br>
<input type="text" name="res" value="<?php echo $res;?>"><br>
<input type="submit" name="sub" value="+">
<input type="submit" name="sub" value="-">
</form>
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>
PHP Code to add two numbers , adding happens as soon as i type the second number in the second field with the result populating in the third field.
Can it be done using php or we do use Ajax or other programming languages.
Would be helpful if i could get an answer for this.
Javascript is enough for this purpose
<script type="text/javascript">
function Adder()
{
var a= parseInt(document.frm.A.value);
var b = parseInt(document.frm.B.value);
var result=a+b;
document.frm.Result.value=result;
}
</script>
<form name="frm">
<input id="A" name="A" type="Text" onchange="Adder()" >
<input id="B" name="B" type="Text" onchange="Adder()" >
<input id="Result" name="Result" type="Text" >
</form>
Using jQuery
$('#field2').change(function (){
if(!isNaN($('#field1').val()) && !isNaN($('#field2').val()))
{
$('#field3').val($('#field1').val()+$('#field2').val())
}
});
Where field1,field2, and field3 are the id of fields. It will put the addition in field3 only if the values entered in field 1 and field2 are numeric.
You can do it using javascript.
Script :
function add(){
var num1 = document.getElementById("num1").value;
var num2 = document.getElementById("num2").value;
sum = num1+num2;
document.getElementById("sum").value = sum;
}
HTML:
<input type="text" id="num1" name="num1" />
<input type="text" id="num2" name="num2" onchange="add();" />
<input type="text" id="sum" name="sum" />
NOTE : this code is not tested please make relevant changes
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>
I am creating a dynamic form where the user will have the ability to add a set of inputs to the form. The html looks like this:
<form>
<input id="title1" class="title" name="title1" type="text" value="">
<input id="productionCompany1" name="productionCompany1" type="text" value="">
<input id="year1" name="year1" type="text" value="">
<input id="role1" name="role1" type="text" value="">
<div id="newCredit"> </div>
add another credit
</form>
When the user clicks the link with the id of "addCredit" the following jQuery script is called:
$(document).ready(function() {
var $ac = $('#addCredit');
$ac.click(function() {
/* the following two lines are where the problem lies? */
var $credInt = $(this).prev(".title");
$.get("addCredit.php", {num: $credInt},
function(data){
$('#newCredit').append(data);});
return false;
});
});
The jQuery function queries a php file called "addCredit.php", which looks like this:
<?php
$int = $_GET["num"];
$int = substr($int, -1);
$int++;
?>
<input id="title<?php echo $int;?>" class="title" name="title<?php echo $int;?>" type="text" value="">
<input id="productionCompany<?php echo $int;?>" name="productionCompany<?php echo $int;?>" type="text" value="">
<input id="year<?php echo $int;?>" name="year<?php echo $int;?>" type="text" value="">
<input id="role<?php echo $int;?>" name="role<?php echo $int;?>" type="text" value="">
My problem is getting the javascript variable $credInt set properly so that it can be sent to the addCredit.php page and update the form fields accordingly. I also need to be sure that every time the form is appended, the next value sent is the incremented value.
Any thoughts on how I might accomplish this? Thank you for your help.
This is the wrong way of doing it; PHP can handle array syntax in a variable name. This makes it much easier to handle. It is also unnecessary to call the server to clone the form. You should name your fields like this:
<form>
<div id="originalCredit">
<input name="title[]" type="text" value="">
<input name="productionCompany[]" type="text" value="">
<input name="year[]" type="text" value="">
<input name="role[]" type="text" value="">
</div>
add another credit
</form>
And then your Javascript can be like this:
$(function() {
$('#addCredit').click(function() {
var newCredit = $('#originalCredit').clone(); // create new set
newCredit.find('input').val(''); // empty input fields
$(this).before(newCredit); // append at the end
return false;
});
});
When the form is finally sent to the server, because the variables are in the format of name[] PHP will recognize they are an array and then you can do this:
<? foreach($_POST['title'] as $k => $v) { ?>
Title: <?=$_POST['title'][$k]?><br>
Company: <?=$_POST['productionCompany'][$k]?><br>
Year: <?=$_POST['year'][$k]?><br>
Role: <?=$_POST['role'][$k]?><br>
<? } ?>
Obviously this is just displaying it as an example, but you can then save/update/whatever with it.