Sorry for the question but, I don't know how to solve my problem.
I've got a form with predefined input names in a standard form and I have other inputs with any random names, for example:
<form action="">
<!-- required inputs -->
<input type="text" name="name" value="Ian" />
<input type="text" name="surname" value="Dope" />
<input type="tel" name="phone" value="782910456" />
<input type="tel" name="comment" value="" />
<!-- optional inputs -->
<input type="hidden" name="conact_amount" value="1" />
<input type="hidden" name="value1" value="2" />
<input type="hidden" name="other_filed" value="3" />
<input type="hidden" name="different_name" value="4" />
</form>
In the above form, the required inputs will have the same name, whereas the optional inputs can be any arrangements of different names.
I want to leave the required fields as they are, but the optional fields - that I don't know the names of - need to be placed in an array called comment.
What about iterating over the posted array as so:
$required = ['name', 'surname', 'phone', 'comment'];
$comment = array();
foreach ($_POST as $key => $value) {
if (!in_array(strtolower($key), $required)) {
$comment[$key] = $value;
}
}
This assumes that you know the names of the required fields ahead of time.
It simply loops through the posted array, determines if it was a required field with a set name attribute and, if it isn't, it will add it to a new array - $comment.
This, of course, assumes that my understanding of your question is correct.
Related
I am trying to understand how to use the $_GET function. I am using a form from my CRM system Infusionsoft. I have a few different ads running to this form from different sources. I use UTM's to keep track of where people are coming from.
Example URL UTM: http://www.awesome.com/?utm_source=google&utm_medium=cpc&utm_term=keywords&utm_content=content&utm_campaign=name
The hidden field was my attempt to try to pull the data and pass it along when the form is submitted. Unfortunately it is not working and I am a bit of a newbie to php.
<form accept-charset="UTF-8" action="https://mk165.infusionsoft.com/app/form/process/ff023ed2f8ffd7c46b03cdc50a115e93" class="infusion-form" method="POST" name="myForm" onsubmit="return validateForm()">
<input name="inf_form_xid" type="hidden" value="ff023ed2f8ffd7c46b03cdc50a115e93" />
<input name="inf_form_name" type="hidden" value="Ninh Neuropathy" />
<input name="infusionsoft_version" type="hidden" value="1.48.0.46" />
<input type="hidden" id="inf_custom_GaSource" name="inf_custom_GaSource" value='<?php echo $_GET['utm_source']?>'/>
<input type="hidden" id="inf_custom_GaMedium" name="inf_custom_GaMedium" value='<?php echo $_GET['utm_medium']?>'/>
<input type="hidden" id="inf_custom_GaTerm" name="inf_custom_GaTerm" value='<?php echo $_GET['utm_term']?>'/>
<input type="hidden" id="inf_custom_GaCampaign" name="inf_custom_GaCampaign" value='<?php echo $_GET['utm_campaign']?>'/>
<input type="hidden" id="inf_custom_GaContent" name="inf_custom_GaContent" value='<?php echo $_GET['utm_content']?>'/>
<input class="infusion-field-input-container" placeholder="First Name" name="inf_field_FirstName" type="text" />
<input class="infusion-field-input-container" placeholder="Last Name" name="inf_field_LastName" type="text" />
<input class="infusion-field-input-container" placeholder="Phone" name="inf_field_Phone1" type="text" />
<input class="infusion-field-input-container" placeholder="E-mail" name="inf_field_Email" type="text" />
<input class="infusion-field-input-container submit" id="submit" name="" value="Get In Touch Today" type="submit" />
</form>
Your form method is POST so when you submit the form you will get the values in php as:
<?php
print_r($_POST);
?>
You will get the all form input values in $_POST Super Global included all hidden inputs.
One last thing also use double quotes in hidden input values instead of single quotes as:
<input type="hidden" id="inf_custom_GaSource" name="inf_custom_GaSource" value="<?php echo $_GET['utm_source'];?>"/>
Often both types of parameters are needed. It might make things easier if GET and POST parameters are normalized and then can be accessed in a unique way like this:
$params = array();
foreach ($_GET as $k=> $v) $params [$k] = $v;
foreach ($_POST as $k=> $v) $params [$k] = $v;
echo $params['myvalue'];
Sorry for grammar mistakes. I have multiple dynamically created text fields (more than 100). how can i get the values of these fields. i don't know the number of fields. Please help me.
Example
<input type="text" name="id_1" />
<input type="text" name="id_2" />
<input type="text" name="id_3" />
................
<input type="text" name="id_100" />
Use arrays:
<input type="text" name="id[1]" />
<input type="text" name="id[2]" />
<input type="text" name="id[3]" />
Then:
foreach($_POST['id'] as $key => $value) {
echo "text $key = $value";
}
If you mean you want to get their values in PHP easily, just name them like "name[]".
<form method="post" action="yourscript.php">
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="text" name="input[]" />
<input type="submit" value="Submit" />
</form>
This way you'll be able to get the values in yourscript.php. They'll be in $_POST['input']. Just loop over it using foreach to get the values:
foreach($_POST['input'] as $value) {
// do what you want with the $value
}
Having index numbers in the names like this:
<input type="text" name="input[1]" />
<input type="text" name="input[2]" />
is redundant.
In HTML forms, using jQuery:
$(function() {
$("input[name^='id']").each(function() {
var name = $(this).attr('name');
var inputValue = this.value;
console.log(name + " : "+ this.value);
});
});
Hoa about adding a hidden field to pass the number of fields to the processing script?
<input type='hidden' name='num_text_fields' valus='100>
The do a for loop to get the values
$num_text_fields = $_POST['num_text_fields'];
for ($i=1;$i<=$num_text_fields;$i++)
{
$text_field_name = "id_" . $i;
$value = $_POST[$text_field_name];
// do something with $valus
}
I think the best way to do it is to add a class element to your input.
Then you can be easily able to find them regardless of how many you have
I'm practicing form validation with JavaScript but when I try to retrieve the data from the page it submits to I can't get it.
form.html
<body>
Hello.<br />
<form onsubmit="return validate()" action="process.php" method="POST">
Enter name: <input type="text" id="name" /><br />
Enter phone number: <input type="text" id="number" /><br />
Enter password: <input type="password" id="paswd" /><br />
Is there anything else you would like to add: <input type="text" id="anything" /><br />
<input type="submit" value="Check Form" />
</form>
</body>
process.php
<?php
echo 'Here: '.$_POST['number']
?>
Whatever index I use I get " Undefined index: line 2". What am I doing wrong?
EDIT: So I can't use the id attribute I need the name? Is there anyway to prevent coding redundancy since the value of all names will be the same as the corresponding id?
You need name attribute in your fields
<input type="text" id="number" name="number" />
$_POST looks for the name attribute in the field to capture the field values and not id
Your inputs need the name of the element.
Such as:
<input type="text" id="number" name="number" />
The Php gets the form data looking these names, not the ids.
you forgot name of input:
<input type="text" id="number" name="number" />
You need to give your form elements names.
<input type="password" id="paswd" name="paswd" />
Interestingly names and ids share the same namespace. If you don't really need the ids, leave them be. Inside a validate function you can always access all elements with the elements object of the form.
// called onsubmit
var validate = function(e) {
if (this.elements["paswd"].value.length < 4) {
alert("password needs to have at least 4 characters");
return false;
}
return true
};
I usually append the input type to my ids to differentiate them from field names
<label for="paswd-txt">Password: </label>
<input type="text" name="paswd" id="paswd-txt" />
<label for="save-cb">Remember me: </label>
<input type="checkbox" name="save" id="save-cb" value="1"/>
So like Vitor Braga said your inputs need the name of the element, but you only need this if you are using PHP to hadle the values of form in the submit, if you are using javascript to validaate like you said your were praticing you can obtain the value like this:
document.getElementById("number").value
I am wondering how I can extract the title variable from my forms posted fields array.
I know $_POST['name'] is the field name but can I do something like $_POST[title] to get the title?
I ask because I have a dynamic form with variable lengths. The dynamics is group_one contains 5 fields, group_two contains 12 fields, group_three contains 2 fields for example.
I am hoping to loop through these groups and post the title of the form field to a column in a DB and its value. Any help appreciated with understanding if I can use the 'title' variable in the form field element.
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesYes" value="Yes" title="Dog at premises" />Yes
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesNo" value="No" title="Dog at premises" />No
<input type="text" name="txtGroupOne[]" id="txtNextOfKinName" title="Next of kin name" />
<input type="text" name="txtGroupOne[]" id="txtNextOfKinContact" title="Next of kin contact" />
No, only the name="x" attribute is sent by default in an HTML Form. title="x" is not sent. You can technically get around it with some crazy Javascript and an Ajax POST, but I would avoid that if I were you.
What do you want the title sent to your server side for? There surely probably a better alternative to achieve your goal.
You can add hidden field to each of fields that need titles to be sent to server besides their values.
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesYes" value="Yes" />
<input type="hidden" name="txtGroupOneTitles[]" value="Dog at premises" >
<input type="radio" name="txtGroupOne[]" id="txtDogAtPremisesNo" value="No" />
<input type="hidden" name="txtGroupOneTitles[]" value="Dog at premises" >
<input type="text" name="txtGroupOne[]" id="txtNextOfKinName" />
<input type="hidden" name="txtGroupOneTitles[]" value="Next of kin name" >
<input type="text" name="txtGroupOne[]" id="txtNextOfKinContact" />
<input type="hidden" name="txtGroupOneTitles[]" value="Next of kin contact" >
You can do something like this:
foreach($_POST as $form_var){
// Compile your code here, etc.
}
Anything that does not have a "name" attribute in your form will not be part of the $_POST array.
ie,
<input type='text' id='bar'>
<input type='text' name='foo' id='foo'>
Only $_POST['foo'] would exist.
Misread the question slightly, but you could also prepend your input names to include the "group title" upon generation.
<input type='text' name='title1_foo' id='title1_foo'>
Then you can manipulate the information you want from the $_POST loop.
There are 10 boxes in my website that I fill up based on my needs. From php code I prefer not to check them one by one like this. Instead of that I thought it would be a good idea to put check marks in each boxes and if I fill something in input field it should be checked so I can check however many checkboxes are filled and know how many boxes are filled.
if ($input1) {$total = "1";
if ($input2) {$total = "2";
}
}
Anybody knows how can I put automatically check into a checkbox when I start typing anything in it ? But it should be unchecked back if I delete what I wrote before sending it. Or if you guys have a better idea that would be nice also.
Thank you !
you could do this in JavaScript. Basically if the number of input text is the same of the checkboxes then you could use this function for example:
function change() {
var input_lengths = document.getElementsByName("textArray[]");
for(var i= 0; i < input_lengths.length;i++) {
if(document.getElementsByName("textArray[]").item(i).value != "") {
document.getElementsByName("checkArray[]").item(i).checked = true;
}
}
}
and your html could be:
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="" />
<input type="text" name="textArray[]" value="AAA" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="checkbox" name="checkArray[]" value="" />
<input type="button" value="test" onclick="javascript:change()" />
why don't you name your checkboxes as an array e.g.:
<input type="checkbox" name="boxes[1]" />
<input type="checkbox" name="boxes[2]" />
then you can loop through the array in php and check each one individually
This is how i understood your problem:
you have 10 text inputs
you want to know how many of them contain actual input when they are submitted
your idea was to assign a checkbox input to each text input so that the corresponding checkboxes are checked when an input has text and is unchecked when an input has no text
Something very quick and dirty. You might want to name you inputs with a square bracket, so that php interprets that parameter as an array. Then you can iterate over an array and if the values are set you can count.
<html>
<body>
<?php
if(isset($_POST['input'])) {
$count = 0;
foreach($_POST['input'] as $value) {
if($value) {
$count++;
}
}
echo $count;
}
?>
<form action="testinputs.php" method="post">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input name="input[]" value="" type="text">
<input type="submit"/>
</form>
</body>
</html>
If you want to do that on the client side, than I'd take something like jQuery and look how many inputs on my page contain any text inside them.