I have n number of text box (n may be any number) with same name. And
I want to access the value of all the text box of that name.
Ex-:
<form method="post" id="create">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>
jQuery
<script>
jQuery(document).ready(function($) {
$('#newFieldBtn').click(function(){
var code='<input type="text" name="user[]" />';
jQuery('#create').append(code);
</script>
or is there any other way to access the value of the text box. Either
by class or any other property..
<script>
jQuery(document).ready(function($) {
$('#newFieldBtn').click(function(){
var count = document.getElementById('count').value;
count++;
var code = '<input type="text" name="user'+count+'" />';
jQuery('#create').append(code);
document.getElementById('count').value = count;
</script>
and your html like...
<form method="post" id="create">
<input type="hidden" id="count" value="0" name="count">
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>
and in your php code...
<?php
if(isset($_POST))
{
$count = $_POST['count'];
for($i=1;$i<=$count;$i++)
{
$user.$i = $_POST['user'.$i];
}
}
?>
Try this one, it will show you the values :
<form action="#" method="post">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="submit" value="submit" >
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST['user'] as $key => $value)
{
echo $key." has the value = ". $value."<br>";
}
}
?>
See this in action: http://wistudat.be/try/array.php
Related
I have a html form with many fields:
<form action="" method="get">
...
<input type="checkbox" name="price" id="price-1000" value="1000">
<input type="checkbox" name="price" id="price-2000" value="2000">
<input type="checkbox" name="price" id="price-3000" value="3000">
...
<input type="submit" value="send">
</form>
On check and send I have the following query string: ?price=1000&price=2000
How make it ?price=1000,2000 in browser URL
I think I need a rewrite rule via htaccess? Or is there some other way?
Using Jquery It will be possible.
index.php
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<form action="redex.php" method="get">
<input type="checkbox" id="price-1000" value="1000" class="ckbox">
<input type="checkbox" id="price-2000" value="2000" class="ckbox">
<input type="checkbox" id="price-3000" value="3000" class="ckbox">
<input type="hidden" name="price" id="price-1000" value="1000" class="ckvalues">
<input type="submit" value="send">
</form>
<script type="text/javascript">
$(document).ready(function() {
var favorite = [];
$('.ckbox').change(function() {
$.each($("input[type='checkbox']:checked"), function(){
favorite.push($(this).val());
});
var str1 = favorite.toString()
$('.ckvalues').val(str1)
favorite.length = 0;
});
});
</script>
redex.php
<?php
echo $_GET['price'];
?>
May this help you.
I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>
I have two text boxes and one button. I want when i will type any value in 1st textbox and click on a button the value which i'll type in 1st text box should be the value of 2nd text box. And i was using this code. Can some one help me on this?
<?php
if(isset($_POST['sum']))
{
$v1=$_POST['abc'];
if($v1=="vivek")
{
echo "welcome Mr.".$v1;
}
else
{
echo "Unauthorised User Mr. ".$v1;
}
}
?>
<html>
<body>
<form method="post" action="">
<p>Name:     
<input type="text" name="abc" value=""/>  
</br></br>
Passed Value: <input type="text" name="xyz" value="<?php echo $v1;?>"/>
</p>
<p>
<input type="submit" name="sum" value=" Submit "/>
</p>
</form>
</body>
</html>
try this one.
<form method="post" action="">
<p>Name:     
<input type="text" name="abc" value=""/>  
</br></br>
Passed Value: <input type="text" name="xyz" value="<?php if(isset($_POST['abc'])) { echo $_POST['abc']; } ?>"/>
</p>
<p>
<input type="submit" name="sum" value=" Submit "/>
</p>
</form>
Passed Value:
<input type="text" name="xyz" value="<?php
if(isset($_POST['abc']))
echo htmlentities($_POST['abc'],ENT_QUOTES);
?>"/>
This should do it :)
Try this code.
<script type="text/javascript">
function sub() {
document.getElementById('xyz').value = document.getElementById('abc').value;
}
</script>
<form>
<label>Amonut</label>
<input type="text" name="abc" onkeyup="sub();" id="abc" />
<br /><br />
<label>Discount</label>
<input type="text" name="xyz" id="xyz" />
<input type="submit" name="sum" value=" Submit "/>
</form>
<script type="text/javascript">
function sub() {
document.getElementById('xyz').value = document.getElementById('abc').value;
}
</script>
<form onsubmit="sub();">
<label>Amonut</label>
<input type="text" name="abc" id="abc" />
<br /><br />
<label>Discount</label>
<input type="text" name="xyz" id="xyz" />
<input type="submit" name="sum" value=" Submit "/>
</form>
I think you want this.
Try this code
$v1='';
if(isset($_POST['sum']))
{
if($v1=="vivek")
{
echo "welcome Mr.".$v1;
$v1=$_POST['abc'];
}
else
{
echo "Unauthorised User Mr. ".$v1;
}
}
When I input numeric value in Number 1 and Number 2, and press "Add". It does not display the total added value. Please see my coding below. and advice me, what to is the problem, and what can be done.
<html>
<head>
<title>Simple Calculator</title>
<?php
if(isset($_POST['submitted'])){
if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])){
$add = ($_POST['number1'] + $_POST['number2']);
echo "Add: ".$_POST['number1']."+".$_POST['number2']."=";
}
}
?>
<script type="text/javascript">
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="post">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if(isset($_POST['number1'])) echo $_POST['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if(isset($_POST['number2'])) echo $_POST['number2'];?>"/></p>
<input type="button" name="add" value="Add" />
<input type="button" name="minus" value="Minus" />
<input type="button" name="multiply" value="Multiply" />
<input type="button" name="divide" value="Divide" />
<input type="reset" name="rest" value="Reset" />
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
You are echoing the result data into the <head>, so it will not be displayed.
You forgot to echo $add.
Your <input>s are of type button and not submit, so the form will not be submitted to the server.
Because you are echoing the previously entered values into the form, <input type="reset"> will probably not do what you want/expect it to do. I think it would be better to implement this as another submit.
Because this form affects only what the next page displays and does not make a permanent change to the server, you should use the GET method and not POST.
Try this:
<html>
<head>
<title>Simple Calculator</title>
<script type="text/javascript"></script>
</head>
<body>
<h1>Simple Calculator</h1>
<form action="simple_calculator.php" method="get">
<p>Number 1: <input type="text" name="number1" size="20" value="<?php if (isset($_GET['number1']) && !isset($_GET['reset'])) echo $_GET['number1'];?>"/></p>
<p>Number 2: <input type="text" name="number2" size="20" value="<?php if (isset($_GET['number2']) && !isset($_GET['reset'])) echo $_GET['number2'];?>"/></p>
<input type="submit" name="add" value="Add" />
<input type="submit" name="minus" value="Minus" />
<input type="submit" name="multiply" value="Multiply" />
<input type="submit" name="divide" value="Divide" />
<input type="submit" name="reset" value="Reset" />
<input type="hidden" name="submitted" value="1" />
</form>
<?php
if (isset($_GET['submitted']) && !isset($_GET['reset'])) {
echo "<div>";
if (is_numeric($_GET['number1']) && is_numeric($_GET['number2'])) {
if (isset($_GET['add'])) {
$result = $_GET['number1'] + $_GET['number2'];
echo "Add: ".$_GET['number1']." + ".$_GET['number2']." = ".$result;
} else if (isset($_GET['minus'])) {
$result = $_GET['number1'] - $_GET['number2'];
echo "Minus: ".$_GET['number1']." - ".$_GET['number2']." = ".$result;
} else if (isset($_GET['multiply'])) {
$result = $_GET['number1'] * $_GET['number2'];
echo "Multiply: ".$_GET['number1']." * ".$_GET['number2']." = ".$result;
} else if (isset($_GET['divide'])) {
$result = $_GET['number1'] / $_GET['number2'];
echo "Divide: ".$_GET['number1']." / ".$_GET['number2']." = ".$result;
}
} else {
echo "Invalid input";
}
echo "</div>";
}
?>
</body>
</html>
The solution of DaveRandom works fine if you change this
action="simple_calculator.php"
by
action="<?php echo $_SERVER['PHP_SELF'] ?>"
This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>