I have a form with list of fields :
last name
email
country
message
Before sending the form, i want to joint to it a static value : destination=marketing.
i don't want it to be visible in my form. how would this be done ?
Input hidden is what you need :
<input type="hidden" name="destination" value="marketing" />
<input type="hidden" name="destination" value="marketing" />
or you can add this variable when you process the form server-side
With a hidden input. Or put it in the session instead.
Related
I'm using CodeIgniter and in a single PHP file with JavaScript inside, I want to pass a JavaScript variable to the body (PHP) and make it a hidden input. But whenever I use the controller to post the value (where the JavaScript variable is), it returns none. Here are some parts of the code:
JS:
function pass() {
//some code
document.getElementById('yes').innerHTML = yes; //where yes is a var
}
HTML (PHP):
<form action="search">
<input type="hidden" name="yes" value="<?php $yes= "<p id='yes'> </p>"; echo $yes;?>" />
<input type="submit" name="yes" value="Done" />
</form>
So whenever I post the yes in the controller $yes = $this->input->post('yes'); it returns nothing.
How can I pass the JavaScript variable so I can use it again in the next file? Thank you!
You did'nt set the form method so it defaults to GET
You should set
<form action="search" method="POST">
try
JS :
var yes = "<?php echo $_POST['yes']; ?>";
document.getElementById('yes').innerHTML = yes;
You have to set the value property of the <input>, not the innerHTML. You also need to give the <input> a different name than other fields or the "submit" button. Finally, you have to give your <input> an "id" property so that you can actually get it with getElementById().
You should be setting the value of the hidden input, not the innerHTML. This code should work:
function pass() {
//some code
document.getElementById('yes').value = yes; //where yes is a var
}
Another problem, as noted by Pointy, is that the hidden input doesn't actually have an id, so you should give it an id (in this case the id should be yes).
Something you should also do is escape the html you are inserting into the hidden input with PHP, so it doesn't accidentally get parsed. You can do this with htmlspecialchars():
<form action="search">
<input type="hidden" id="yes" name="yes" value="<?php $yes= htmlspecialchars("<p id='yes'> </p>"); echo $yes;?>" />
<input type="submit" value="Done" />
</form>
Your submit button and your hidden field have the same name
yes .
You try to access your hidden input by id yes , and your input
does not have this id , use getElementByName('yes') instead or give
your hidden field id='yes'.
You use innerHtml which only sets or returns the inner HTML of an element,it should be value.
HTML CODE:
<form action="search">
<input id='yes' type="hidden" name="yes" value="<?php $yes= "<p id='yes'> </p>"; echo $yes;?>" />
<input type="submit" name="yess" value="Done" />
</form>
JS :
document.getElementById('yes').value = yes;//yes is a variable
I am taking input from user in a form. Can I get that value(user input) to calculate other fields in the same form.
<input size="12" id="inputField" name="inputField" autofocus="" type="date" onblur="return dateValidate(this)"/>
Can I collect this form input later in the form and use it to calculate other fields. I was trying to using to use $_POST to retrieve the value but I am not sure if thuis is the right thing.
You cannot do it in PHP before submitting the form. You can easily do so in JavaScript. However you could simply add some AJAX code to send the value to your PHP script when user enters something in the box, and parse the response accordingly. PHP works on server side, and does not interact with user without any server side request
You can certainly do it in 2 ways...
1st way
<?php
$first_digit = '';
$second_digit = '';
$third_digit = '';
if(isset($_POST['calculate'])) {
$first_digit = $_POST['first_digit'];
$second_digit = $_POST['second_digit'];
$third_digit = $first_digit + $second_digit;
}
?>
<form method="POST">
<input type="text" name="first_digit" value="<?php if(isset($first_digit)) echo $first_digit; ?>" />
<input type="text" name="second_digit" value="<?php if(isset($second_digit)) echo $second_digit; ?>" />
<input type="text" name="third_digit" readonly value="<?php if(isset($third_digit)) echo $third_digit; ?>" />
<input type="submit" name="calculate" value="Calculate" />
</form>
2nd Way
Total the variables in the code and instead of showing the result output in a text box you can instead calculate and echo out the result, or you can store that in the database.
Note: Be sure you keep the third input[type=text] as readonly as it is showing you the calculated value so probably you don't want your users to change
Yes you can get input from user to calculate other fields in the same form, you should use scripts like OnChange() or onClick() to have this.
I have a page that allows the user to add and remove text fields to a form using JavaScript.
Text fields are named field1, field2, field3, etc. and depends on how many fields the user has added
I'm trying to store all the values from my text fields into one Php variable;
I understand that i need to store them into an array first and then use implode(), but how can i specify how many inputs there are within my Php code?
Usually the best way to approach this is to use array-named input, as shown in the following example in the PHP docs:
<form action="" method="post">
Nombre: <input type="text" name="personal[nombre]" /><br />
Email: <input type="text" name="personal[email]" /><br />
Cerveza: <br />
<select multiple name="cerveza[]">
<option value="warthog">Warthog</option>
<option value="guinness">Guinness</option>
<option value="stuttgarter">Stuttgarter Schwabenbräu</option>
</select><br />
<input type="submit" value="submit me!" />
</form>
You could use the very same name for each of the user added fields, as in:
<input type="text" id="field1" name="fields[]" />
<input type="text" id="field2" name="fields[]" />
And then just use implode as required:
$imploded_fields = implode(', ', $_POST['fields']);
There are many options:
You can use cookies. Use PHP $_COOKIE to get it. For help.
You can use html hidden input fields - <input type="hidden" value=""> and can store actual number of fields in it.
But, Diego Agulló is better one
You can create a hidden field and initialize it with 1 if on option is already open(field1). And increase the the value of counter while increasing the value of fields and vise verse. On submit you will find the total fields added.
Thanks
I have a text box which picks up a value from my database and is a read only text box. it contains some information which i want to send to my datbase but nothing is being recorded. Once the textbox isnt a read only, the data is successfully stored. Is there any way i can keep my textbox disabled and still send the data to my database?
<form action="refnoadded.php?public=<?php echo $id; ?>" method="post">
<span id="sprytextfield1">
<input type="text" name="ref" value="<?php echo $newpass ?>" disabled />
<input type="button" onClick="history.go(0)" value = "Generate Reference">
<br />
<span class="textfieldRequiredMsg">Reference Number Required</span>
</span>
<br />
<input type="submit" value="Add Reference" />
</form>
any ideas?
disabled doesn't send data to server
use
<input readonly value="my value" name="myname">
HTH!
have the data in a second hidden input:
<input type="hidden" name="ref_hidden" value="<?php echo $newpass ?>" />
the user won't see the difference and you will get your value send when submitting the form.
Disabled textfields don't submit their information to $_POST or $_GET. You can simply use the form element
<input type="hidden" name="rev_hidden" value="<?php print $password; ?>" />
This is the standard (correct) way to pass hidden information in the form.
Another use for this element is if you want to pass a "formsubmitted" variable.
However, if you want to create a value and have it uneditable by the user, do it on the server side. Create the value when you create the database, since users can relatively-simply send other data in the place of what you've generated.
I am using a checkbox in my code, I want to add a readonly property to the textbox, but I saw that readonly property cannot be added to checkbox. The suggested alternative is to set disabled to true.
But the case is if i set disabled property to true. I cannot get the checkbox value server side. I want a solution that sets the checkbox readonly and can get value in server side.
How can I do this?
This is my view:
<input id="AddNewProductCategory" class="" type="checkbox" tabindex="1900" name="addnewproductcategory" value="1" checked="checked">
JavaScript:
$(document).ready(function(){
jq('#AddNewProductCategory').attr('checked',true);
});
In my server:
$check_box_val = $this->ci->input->post('addnewproductcategory');
I am using CodeIgniter and jQuery.
You could add a hidden field -
<input id="AddNewProductCategoryHidden" class="" type="hidden" tabindex="1900" name="addnewproductcategoryhidden" value=""/>
then set that when the checkbox gets clicked -
$(document).ready(function(){
jq('#AddNewProductCategory').attr('checked',true);
jq('#AddNewProductCategoryHidden').val('true');
});
Insert in HTML: <input type="hidden" name="addnewproductcategory" value="1" /> and disable the checkbox.
I would add a <input type='hidden'> with the value 1 or 0 and disable the checkbox