PHP Problem

calumn

New Member
Hello.

I am writing a php script for someone. The script will let the user create html forms that email it to their email.

They enter their email and how many questions they want and hit submit.

It then goes to the next part where a loop creates text boxes on the screen that the user will type the questions in and choose whether they want the questions to have a text box or a large text area.

They then hit submit and it goes to the php page which will create the html form file and a php script that will email the results to them. So far I am trying to get the script to create the html form.

The code is working and doesn't give a php error, but whenever the user chooses a large text area everything after that is automatically put into the large text area and no more html is created.

This is the php code:

PHP:
<?php

require("../config.php"); //require config file for details of admin (email etc._

$fields = $_REQUEST['fields'] ; //fields turned from array to standard variable
$formname = $_REQUEST['formname'] ; //name of form turned from array to standard variable
$formemail = $_REQUEST['formemail'] ; //email of form turned from array to standard variable
$formaccessname = $_REQUEST['formaccessname'] ; //access name turned from array to standard variable

echo'Writing form file 1.';

$filename = "../" . $formaccessname . ".html"; //creates the file with the name the user chose
$newfile = fopen($filename, "w+") or die ("Couldn't access file (is it writable?)"); //opens the file
        $newstring .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'; //adds html to the file
		
        $newstring .= '<html xmlns="http://www.w3.org/1999/xhtml">'; //adds html to the file
		
        $newstring .= '<head>';//adds html to the file

        $newstring .= '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />';//adds html to the file

        $newstring .= '<title>' . $formname . '</title>';//adds html to the file
		
        $newstring .= '</head><body>';//adds html to the file
	
        $newstring .= '<form action="' . $formaccessname . 'process.php" method="post">';//adds html to the file

$i=1; //start of counter for while loop
while($i<=$fields) //the while loop loops until it has created all the fields the user wanted
  {
	 $newstring .= $_REQUEST['field' . $i . 'name'] . ': ';//Writes the name of the field to the html page (eg. Name:)
   if ($_REQUEST['field' . $i . 'type']=="text"){ //finds out whether user wants a text area or text box, if it is text box it does this:
   
  $newstring .= '<input name="'; //start of textbox
  $newstring .= $_REQUEST['fieldshort' . $i . 'name']; //writes the name of the field from the previous form, the $i is so it knows which number
  $newstring .= ' type="text" /><br />'; //end of field, new line
  
}  
else{ //if it isn't a textbox
$newstring .= '<textarea name="'; //start of textarea
  $newstring .= $_REQUEST['fieldshort'" . $i . 'name']; //writes the name of the field from the previous form, the $i is so it knows which number
  $newstring .= ' cols="40" rows="6" /><br />'; //end of field, new line
  }


  $i++; //adds 1 to the counter
  }
        $newstring .= '<input name="Submit" type="submit" value="Submit" /></body></html>'; //end of html
		
        @fwrite ($newfile, $newstring) or die ("Couldn't write config.php file (is it writable?)"); //writes the file
        fclose ($newfile);  //new file

$i=1; //sets i to 1
while($i<=$fields)
  {

  //space to create the php process page when I need it

  $i++;
  }

 

?>

I have added lots of comments so you know what it is doing.

This is a page which shows what happens to the form when it is created:
http://www.computalk.net/thegrrlforms/form3.html

As you can see, all the html code after the text area is inside the text area.

I would be very grateful for any help.

Thanks
Calum
 

apj101

VIP Member
should this bit
$_REQUEST['fieldshort'" . $i . 'name'];

read
$_REQUEST['fieldshort' . $i . 'name'];
 

calumn

New Member
Thanks but nope the same thing is still happening.

I think its happening because of the quotes inside the html mixing from the quotes of the _REQUEST array.

I might try and make a loop that turns all of the different parts if the $_REQUEST array into normal variables and then just put them in.

Do you think that would work?
 

apj101

VIP Member
no, the problem is that you are not teminating the name variable
this is happneing in BOTH you text boxex, but only causes the error in the textarea,
your should make sure that after extracting the textbox or textarea name from the _Resquest array you also concatinate in a ".
ill do the textarea one for you

PHP:
$newstring .= '<textarea name="'; //start of textarea
  $newstring .= $_REQUEST['fieldshort' . $i . 'name']; //writes the name of the field from the previous form, the $i is so it knows which number
 $newstring .= '"'
  $newstring .= ' cols="40" rows="6" /><br />'; //end of field, new line
i added the correction as a new line, but you could just build it into the line were you define the ize of the textarea
both of my sugestions (from the other post) should be implemented
and you need to this for the text box as well

I think that should help
 

Cromewell

Administrator
Staff member
Holy smokes! Use escape ("\" hey look a \" inside of \"s"; ) to put quotes inside of quotes, all of this '"'''""""'''"""'''"" stuff is confusing me :p

I'm pretty sure apj has it but I would do it this way:
Code:
$newstring .= '<textarea name="'; //start of textarea 
  $newstring .= $_REQUEST['fieldshort' . $i . 'name']; //writes the name of the field from the previous form, the $i is so it knows which number 
  $newstring .= '" cols="40" rows="6" /><br />'; //end of field, new line

It's exactly the same but doesn't have a whole line dedicated to adding the closing " :)
I think apj suggested the same thing at the end but that would require even more reading and it's 5am.....
 

calumn

New Member
Thanks, I have done that for both the textarea and textbox and this is the code:

PHP:
  $newstring .= '<input name="'; //start of textbox
  $newstring .= $_REQUEST['fieldshort' . $i . 'name']; //writes the name of the field from the previous form, the $i is so it knows which number
  $newstring .= '" type="text" /><br />'; //end of field, new line
  
}  
else{ //if it isn't a textbox
$newstring .= '<textarea name="'; //start of textarea
  $newstring .= $_REQUEST['fieldshort' . $i . 'name']; //writes the name of the field from the previous form, the $i is so it knows which number
  $newstring .= '" cols="40" rows="6"/><br />'; //end of field, new line
  }

but is still gives the form like this:

http://www.computalk.net/thegrrlforms/form19.html

The source code of the actual html form looks fine but it still doesn't work:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title></title>
</head>

<body>
<form action="process.php" method="post">
field1: <input name="field1" type="text" /><br />
field2: <textarea name="field2" cols="40" rows="6"/><br />
field3: <input name="field3" type="text" /><br /><input name="Submit" type="submit" value="Submit" />
</body>
</html>

Thanks for your help
 

Cromewell

Administrator
Staff member
Oh I know what it is, textarea needs a closing tag.
<textarea>lookie text inside</textarea>
 

calumn

New Member
Ah thanks so much Cromewell, it works now, I didn't know they needed closing because I just added /> at the end.

Thanks loads.
 

apj101

VIP Member
It's exactly the same but doesn't have a whole line dedicated to adding the closing "
I think apj suggested the same thing at the end but that would require even more reading and it's 5am.....
hehe i did, i put in on a new line so it was easy to see the correction i made....hard to spot a single " difference between 2 long lines :D

glad thats all cleared up
 
Top