Add text inputs based on user's requirement - php

<form action = "numbericalInput.php" method = "Get">
Please enter the number of input areas you wish
<input type = "text" name = "amountOfEntry"/>
<input type = "submit" name = "GO"/>
</form>
<?php
if(!empty($_GET("amountOFEntry")){
for($i = 0; $i < $_GET("amountOFEntry"); $i++){
<input type= "text" name = "nums[]" size = "2" />
}
}
?>
What I'm trying to do is ask the user to input a value in to the text area and then for me to present them with an appropriate amount of text inputs for them to enter their values in. So the user enters 10, they have 10 text inputs presented and a submit button or something. I appreciate this line won't work where it is
<input type= "text" name = "nums[]" size = "2" />
but I am sure that's along the right sort of lines? also, what is wrong with this line?
if(!empty($_GET("amountOFEntry")){
thanks

use: isset() http://php.net/manual/en/function.isset.php
<form action = "numbericalInput.php" method = "Get">
Please enter the number of input areas you wish
<input type = "text" name = "amountOfEntry"/>
<input type = "submit" name = "GO"/>
</form>
<?php
if(isset($_GET['amountOfEntry'])){
for($i = 0; $i < $_GET['amountOfEntry']; ++$i){
?><input type= "text" name = "nums[]" size = "2" /><?
}
}
?>
This will check for the existence of $_GET['amountOFEntry'] (Note square brackets as $_GET and $_POST are arrays)
Please also note use of ++$i instead of $i++. There is a minor performance increase here. Not much but it worth doing.
EDIT:::
Please note that the variables will be case sensitive, You are using amountOfEntry in the form and $_GET['amountOFEntry'] in the loop. (Note capitol F)

$_GET is an array, you have to use [] to get the elements. So:
if(!empty($_GET['amountOFEntry']){

As pointed out $_GET returns an array of values. So use the square brackets to find the variable you want. Also you cant mix HTML amd PHP. So you need to make the HTML a string (by quoting it) and user echo (or print) to output the string.
if(!empty($_GET["amountOFEntry"]){
for ($i = 0; $i < $_GET["amountOFEntry"]; $i++) {
echo '<input type= "text" name = "nums[]" size = "2" />';
}
}
Also, as noted by Lizard, you should use isset to determine if the variable is set.

You might as well get the numerical value of the $_GET to avoid runtime errors:
intval($_GET['amountOFEntry'])

If you preferred you could use JavaScript. Using a library like JQuery would help a lot.
JavaScript:
$("#goButton").bind("click",function(e){
numberOfEntries = parseInt($("#numberOfEntries").attr("value"));
for(i=0;i<numberOfEntries;i++){
newInput = document.createElement("input");
$(newInput).attr("type","text").attr("name","nums[]").attr("size","2");
$("#inputEntries").append(newInput);
}
}
);
HTML:
<body>
<input id="numberOfEntries" type = "text" name = "amountOfEntry"/>
<input id="goButton" type = "submit" name = "GO"/>
<div id="inputEntries"></div>
</body>
This is a lot of work just to avoid sending the page back to the server and having the work carried out on the server-side, but thought I might suggest it anyway...

Related

Error passing php variables to another page via form data

I want to pass a variable to another page by form data. I've looked at other tutorials and codes, i've followed them closely, but the contents of the variable doesn't output. Basically php doesn't work inside the value="". Here's the code.
page 1.
<?php
$hi = 1224;
?>
<form method = "post" action = "page2.php">
<input type = "hidden" Name = "var" Value = "<?phpecho$hi;?>"/>
<input type = "submit" Name = "enter" Value = "Submit"/>
</form>
page 2.
<?php
$test = $_REQUEST['var'];
echo $test;
?>
Nothing is outputted.
I've even tried session variables but somehow they worked but once i refreshed the page, the variables were reset. I've started sessions on all pages etc..
It should be <?php echo $hi; ?> instead of <?phpecho$hi;?>. i.e. you need give space between <?php and echo and its $hi variable as like in below.
<input type = "hidden" name = "var" value = "<?php echo $hi; ?>"/>
When something isn't working, make sure to check the source code for error, don't just look browser's rendering. In the source, you'd clearly have seen that <?phpecho$hi;?> wasn't evaluated, but instead just printed in the source.
So in conclusion, <?phpecho$hi;?> is wrong. Write <?php echo $hi;?>.
Change your line
<input type = "hidden" Name = "var" Value = "<?phpecho$hi;?>"/>
to
<input type = "hidden" name = "var" value = "<?php echo $hi; ?>" />
If possible change the name of the hidden textbox to something relevant instead of var , because var is a reserved word in javascript to avoid confusion.
Instead of
<input type = "hidden" Name = "var" Value = "<?phpecho$hi;?>"/>
Should be, you need spaces in php
<input type = "hidden" name = "var" value = "<?php echo $hi; ?>"/>
try this look to $_POST super global for value :
<?php
$test = $_POST['var'];
echo $test;
?>

get checkbox value in to a variable

This is my checkbox
<input name="interests2" type="checkbox" value="double-deep-racks" />
This is how I am trying to get that value in to a variable
$int = $_POST['interests2'];
Can you please tell me what i am doing wrong. I cant get the values I just get blank.
Try
$int = $_POST['interests2'];
If you are trying to set multiple checkboxes you can do something like,
// Your html
<input type="checkbox" name="interests[]" value="This is i">
<input type="checkbox" name="interests[]" value="Another i value">
// php
$email = "Further Information In: \n";
foreach($_POST['interests'] as $i)
$email .= $i . "\n";
The name of your checkbox is interests2. You must get the value by that name like this:
$int = $_POST['interests2'];
The name element must match what you are looking for. In your input field the name is interests2 but you are looking for interests (missing "2").
Also, you may possibly need to look in $_GET instead of $_POST, depending on the form or the AJAX method you are using (you didn't post that portion of your code).

counting number of checkboxes check in email

Here is my php code:
$tasks = ' ';
$help = $_POST['help'];
if(empty($help))
{
$tasks = "None selected.";
}
else
{
$N = count($help);
$tasks = $N;
}
And the HTML is:
<input type="checkbox" name="help" value="sign"> //with several inputs with different values
On the form submit, it emails and outputs everything appropriately except the count of the array. It outputs the $tasks variable at the end of the email always as 1, except when no check boxes are selected. Any combination of selecting checkboxes (1-6) ends up with an array of 1 length. Anyone know why? Thanks!
You'll need to make the checkboxes an array. Change the name to:
<input type="checkbox" name="help[]" value="sign">
You should change your HTML code to:
<input type="checkbox" name="help[]" value="sign">
so that help will be an array. If you only use help, $_POST['help'] will only contain the last value.
you have to rename fields name="help[]" so it can be parsed as array.

passing html information through javascript to php

I have two textboxes that are normally disabled. When a user presses a edit button the textbox becomes enabled and they are allowed to type. However when I submit the form the value that was input by the user is not passed to my php code.
Here is my javascript code:
if (timesin%2 == 0){
document.getElementById(score1).disabled = true;
document.getElementById(score2).disabled = true;
} else {
document.getElementById(score1).disabled = false;
document.getElementById(score2).disabled = false;
document.getElementById(score1).value = "";
document.getElementById(score2).value = "";
}
timesin++;
Here on each alternate clicks it disables or enables, probably not the best way to do this but thats not the important part here.
This is the html code for the box
<input type = 'textbox' id = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' name = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' maxlength = '2' disabled style = 'width:15px; text-align:center' value = '".$col_value."' />
So it is disabled. When a button is clicked the function is called and the textbox is enabled. The user then types a value and clicks the edit button again which disables the textbox. So now I have the textbox disabled with a new inputted valued. However when I submit and get the value through php I get a blank variable.
Any ideas?
Thanks,
<input type = 'textbox' id = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' name = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' maxlength = '2' disabled style = 'width:15px; text-align:center' value = '".$col_value."' />
I see a couple of problems. One is that .$columncount.$inc.$leaguesarray[$numofleagues]. isn't HTML. You need the php tags around it and also need to echo those values, which would look like this:
'columntext<?php echo $columncount.$inc.$leaguesarray[$numofleagues] ?>'
Same thing with this line:
value = '".$col_value."'
should be
value = '<?php echo $col_value ?>'
I'm surprised the page rendered, unless you were preparing it within a php echo all along? Either way, clarifying that bit might help narrow down what the problem could be.
I'm not clear enough but... any disabled input is not going to be sent, if you want to be no-editable try with readonly='readonly', and BTW you should make some kind of validation in your php code, don't trust in JS only
good luck
This is an incorrect way of using inline PHP:
<input type = 'textbox' id = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' name = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' maxlength = '2' disabled style = 'width:15px; text-align:center' value = '".$col_value."' />
Instead try something like this:
<?php
$id = "columntext".$columncount.$inc.$leaguesarray[$numofleagues];
?>
<input type="textbox" id="<?php echo($id); ?>" name="<?php echo($id); ?>" maxlength="2" value="<?php echo($col_value); ?>">
Also, you should consider using a separate CSS file to hold your styling rather than place it within the HTML element. It makes life so much easier and helps keep your design DRY.
Assuming this is inside a PHP echo statement, change
<input type = 'textbox' id = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' name = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' maxlength = '2' disabled style = 'width:15px; text-align:center' value = '".$col_value."' />
to
<textarea id = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' name = 'columntext".$columncount.$inc.$leaguesarray[$numofleagues]."' maxlength = '2' disabled style = 'width:15px; text-align:center' />".$col_value."</textarea>
ive never seen <input type = 'textbox'> before

How to pass data from form without a form field? (PHP)

I have a form for editing a users name and email.
So when it updates the name and email, it needs the username to identify which row it should update.
So i wanted to know if there is any element which is passed with the form but without showing the value or being editable in the input tag.
So i get the username from one script.
The edit user script gets the name and email from the database with the specified username.
Then it passes that new name and email with the username to another script which updates it.
I believe you are looking for
<input type='hidden' name='username' value='theusername' />
hidden - can only be seen in the source of your HTML document
name - where it will be in the $_REQUEST/$_POST/$_GET ($_POST or $_GET depending on how you are submitting your form) variable on submit
value - the username you want this form to relate to
PRO TIP: Have a way to tell who is trying to update users so you don't have unauthorized people updating your user information. It would be very easy for someone to change the username in the form and try to update someone else.
You can use input type hidden
<input type="hidden" name = "username" value="<?php echo $username ?>">
use an:
<input type="hidden" />
HIDDEN is a TYPE attribute value to the INPUT element for FORMs. It indicates a form field that does not appear visibly in the document and that the user does not interact with. It can be used to transmit state information about the client or server. Hidden fields often store a default value (e.g.via php), or have their value changed by a JavaScript.
more here
Use a hidden input tag:
<input type='hidden' name='username' value='theusername' />
As all the others stated you need a hidden input. It WILL be editable though, never trust it as you never trust any other data coming from outside.
But I'd like to add that it would be nicer not to use the username for identifying a row, add an ID column as a primary key instead to your database (possibly auto incremented), and use that in your form.
Something like
<input type="hidden" name="userid" value="<?=$userid?>" />
Arun, you can use GET to pass variables from one page to another page. Simply construct URLs as edituser.php?username=arun and so on. This is the only possible way to pass on variables or data, of course apart from cookies, to other pages w/out using form tags.
Second method is to use JavaScript to create a hidden form field and update it with username.
Third one is to simply add hidden input tags. But this and latter will require form tags.
A word of caution, filter user inputs, be JS, GET or hidden fields.
You can use a hidden form field:
<input type="hidden" name="originalUsername" value="something" />
This won't render on the form in the browser and will likely be ignored and unnoticed by the user.
However, be aware that this is editable. Do not rely on this as a security measure. When the form is submitted, make sure that the user submitting the form (using whatever authentication and authorization mechanisms you have in place) is authorized to make this change before persisting it to the database. Any form field being submitted can be edited.
Use this if you want to use it safely:
<input type='hidden' name='username' value='<?php echo encode("Please Encode Me!","This is a key"); ?>' />
wich will result into:
<input type='hidden' name='username' value='p3e4e4241674d2r4m4i5o464a4f2p3k5c2' />
and in the modification script you will have to use:
<?php $username = decode("p3e4e4241674d2r4m4i5o464a4f2p3k5c2","This is a key"); ?>
Below you have the PHP functions for the ENCODE/DECODE:
<?php
function encode($string,$key) {
$key = sha1($key);
$strLen = strlen($string);
$keyLen = strlen($key);
for ($i = 0; $i < $strLen; $i++) {
$ordStr = ord(substr($string,$i,1));
if ($j == $keyLen) { $j = 0; }
$ordKey = ord(substr($key,$j,1));
$j++;
$hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
}
return $hash;
}
function decode($string,$key) {
$key = sha1($key);
$strLen = strlen($string);
$keyLen = strlen($key);
for ($i = 0; $i < $strLen; $i+=2) {
$ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
if ($j == $keyLen) { $j = 0; }
$ordKey = ord(substr($key,$j,1));
$j++;
$hash .= chr($ordStr - $ordKey);
}
return $hash;
}
?>

Categories