Jump to content

form handling


kev wood

Recommended Posts

Are you looking for this to be done after the form is submitted via PHP or by a MySQL/PostgreSQL?

 

In PHP it is so simple - except of course it isn't done until information is sent back to the server.

 

The easiest way to concat fields is:

 

$field1 = "The";

$field2 = "world";

$field3 = "is";

$field4 = "round";

 

$combined = $field1 . $field2 . $field3 . $field4;

echo $combined; // DISPLAYS "TheWorldisround"

 

If it is MySQL that will be handling the concat then I would suggest visiting this site:

 

http://www.java2s.com/Tutorial/MySQL/0460__String-Functions/0160__CONCAT.htm

Link to comment
Share on other sites

php is what i am used to using but i am looking into using javaScript to process forms. 

 

i want to have a form which has 6 form fields for catching information on a user.  when the user submits the form i want to use javaScript to store the results in an array and then display them in a text area at the bottom of the form.  this will eventually be improved but for now i am startinig easy.

Link to comment
Share on other sites

this is what i have up to now bare in mind i am new

 

<html>
<title>JavaScript Exercise 2</title>
<head>
<script>
<script language="javascript" type="text/javascript">
function= alertName(){
            var array=new Array();
            array[0]=document.getElementById.box1;
            array[1]=document.getElementById.box2;
            array[2]=document.getElementById.box3;
            array[3]=document.getElementById.box4;
            array[4]=document.getElementById.box5;
		array[5]=document.getElementById.box6;

		for(var i=0;i<array.length;i++){
                document.write(array[i]+"<br>");
            }
            document.write("Length of the array is: "+"<br>");
            document.write("==================="+"<br>");        
            document.write(array.length+"<br>");
}
</script>
</head>

<body>

<form name="nameForm">
<input type="text" name="box1">
<input type="text" name="box2">
<input type="text" name="box3">
<input type="text" name="box4">
<input type="text" name="box5">
<input type="text" name="box6">  
<input type="button" value="Submit" onclick="alertName()">  
<input type="text" name="textResult" >

</form> 

</body>
</html>

Link to comment
Share on other sites

In that case...

 

Found this on http://www.w3schools.com/jsref/jsref_concat_array.asp

 

<script type="text/javascript">

var arr = new Array(3);
arr[0] = "Jani";
arr[1] = "Tove";
arr[2] = "Hege";

var arr2 = new Array(3);
arr2[0] = "John";
arr2[1] = "Andy";
arr2[2] = "Wendy";

document.write(arr.concat(arr2));

</script>

Link to comment
Share on other sites

You do not want to use document.write.  That function rewrites the document (i.e., the web page) from the beginning.  So, instead of having the form fields' values added to the end of the page, all you'd get as output would be those values and nothing else.

 

I'm not sure why you'd want to use JavaScript for this, at least, with the way you're describing what you want to do.  Since you're waiting for the form to be submitted, you gain nothing by printing the values to the screen using JavaScript.  There's still that page load/reload when the form is submitted that you have to take into account.  Unless you're using AJAX, there really isn't much of a point using JavaScript instead of PHP for this.

Link to comment
Share on other sites

i have got it working how i want for this section.  here is the code i have now

 

<html>
<head>
<title> Javascript Excercise 2 </title>
<script>
function formfield(){

}
function validate(){
var array = new Array();  //this is declaring the array
array[0]=document.getElementById("text1").value;  //this populating the array elements
array[1]=document.getElementById("text2").value;
array[2]=document.getElementById("text3").value;
array[3]=document.getElementById("text4").value;
array[4]=document.getElementById("text5").value;
array[5]=document.getElementById("text6").value;

var ele = document.forms["form1"].elements["textresult"];  //this section of the code now declares a variable for looping through the array elements to print in the box
ele.value = "";
var  x, len = array.length;  //this section of the code works out the length of the array and sets the var len and x to this
for (x=0; x<len; ++x)  //start of the for loop to print in the array elements into the form field
{
    if (ele.value.length > 0)
        ele.value += "\n";
    ele.value += array[x];
}
}
</script>
</head>

<body>

<form name="form1">
Text 1: 
<input type="text" id="text1">
<br>
Text 2: 
<input type="text" id="text2">
<br>
Text 3
<input type="text" id="text3">
<br>
Text 4
<input type="text" id="text4">
<br>
Text 5
<input type="text" id="text5">
<br>
Text 6
<input type="text" id="text6">
<br>
Text Result
<input type="button" value="submit" onclick="validate()">
<input type="text" id="textresult" name="Text Result">
<input type="button" value="New Field" onclick="formfield()">
</form>

</body>
</html>

 

i am now trying to create a function that will now add a new text input to the form on the click of a button.  for this to work will i need to create the whole form using javascript.

Link to comment
Share on other sites

i have got this working nearly how i wwould like but the it aint quite working exactly write.  it should work by you enter text or numbers in the text fields and then submit the form and it puts all the text or numbers in the text box below the submit button.  this worked originaly but i then added code to add a form field and to concat this data to the other data in the box below the submit.  it does this but it wil not submit the original form until the new text field is displayed and had a number entered into it.  here is the code

 

<html>
<head>
<title> Javascript Excercise 2 </title>
<script>
function validate(){
var array = new Array();  //this is declaring the array
array[0]=document.getElementById("text1").value;  //this populating the array elements
array[1]=document.getElementById("text2").value;
array[2]=document.getElementById("text3").value;
array[3]=document.getElementById("text4").value;
array[4]=document.getElementById("text5").value;
array[5]=document.getElementById("text6").value;
array[6]=document.getElementById("text7").value;

var ele = document.forms["form1"].elements["textresult"];  //this section of the code now declares a variable for looping through the array elements to print in the box
ele.value = "";
var  x, len = array.length;  //this section of the code works out the length of the array and sets the var len and x to this
for (x=0; x<len; ++x)  //start of the for loop to print in the array elements into the form field
{
    if (ele.value.length > 0)
        ele.value += "\n";
    ele.value += array[x];
}
}

function newTextBox(element)
{		
	textField = document.createElement('INPUT');
	textField.type = 'text';
	textField.id ='text7';
	form1.appendChild(textField);
}
</script>
</head>

<body>

<form name="form1">
Text 1: 
<input type="text" id="text1">
<br>
Text 2: 
<input type="text" id="text2">
<br>
Text 3
<input type="text" id="text3">
<br>
Text 4
<input type="text" id="text4">
<br>
Text 5
<input type="text" id="text5">
<br>
Text 6
<input type="text" id="text6">
<br>
<br>
Text Result
<br>
<br>
<input type="button" value="submit" onclick="validate()">
<br>
<br>
<input type="text" id="textresult" name="Text Result">
<br>
<br>
<input type="button" id="btnAdd" value="New text box" onclick="newTextBox();" />
</form>

</body>
</html>

Link to comment
Share on other sites

Save yourself some cross-browser trouble with creating the textarea in JS. Just have it there originally, name it, give it an id and set it to hidden.

 

When you submit the form, grab that with document.getElementById() and set its value to the processed values that you came up with.

 

Now you dont need to create an array of values then loop through it to create the new value list, it is kind of wasteful. Just create a variable and append the values of the fields to that variable.

 

var variable;

 

variable += document.getElementById().value(); //etc

 

When youre done with that set the hidden text area's value to that variable.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.