Solved

Some Forms Problems.

Posted on 2002-03-18
17
222 Views
Last Modified: 2006-11-17
HI All,
I'm facing 2 problems and I wish if anyone one can help.
I designed forms for writing a question (by using PHP+MYSQL). Each form is used to write a specific type of questions (5 types are available).
1- The form that is used to write a multiple-choice question contains the question itself and the answer choices, the form will only display 4 choices BUT I want to give the user the ability to add more choices by clicking on a button (for example) so how this can be done??

2- One of the forms will be used to write a matching question!! Does anyone has any ideas on how this thing can be implemented or even designed??

Your responses will be really appreciated,
Lina
0
Comment
Question by:lina2401
  • 8
  • 7
  • 2
17 Comments
 
LVL 5

Expert Comment

by:andriv
Comment Utility
Don't understand exactly what you are asking but:

1) You can have a radio butoon text field pairs and name them other1, other2 ...

2) You will need a text field for the question and if you are going to give the ability to add the option you'll will need text boxes for each.
0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
You can also ask the question, "How many more choices do you wish to add" then when they submit you can create the text boxes dynamically using associative array:

How Many More Choices do you wish to add <input type=text size=2 maxlength=2 name="numb">

<input type=submit value="add more">

The script will then have:

for($i=1; $i <= $numb; $i++)
{
print('Option $i: <input type=text name="option[$i]"><br>'\n);
}


To pull the options use the foreach

foreach($option as $key => $value)
{
print('Option $key was $value<br>');
}

0
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
First thing can be done better using Javascript.

I dont understand your second requirement.

Please elaborate.

JD
0
 

Author Comment

by:lina2401
Comment Utility
Hi advin, its nice to see you again :))
That script worked as expected BUT my only problem now is that I can't let those 3 buttons work properly in one form.
The buttons I'm talking about are: Preview Question, Add More Choices, & Submit (which will store the question in the database).
Each button has to do a specific thing and I don't know how this can be handled in one form!!
0
 

Author Comment

by:lina2401
Comment Utility
JD,
I guess I better put the second question this in another place different from this!!
what do u think?
0
 
LVL 5

Expert Comment

by:dkjariwala
Comment Utility
foreach($option as $key => $value)
{
print('Option $key was $value<br>');
}

This wont print value of $key and $value but would print literal string $key and $value. It should be,

print("Option $key was $value<br>");

JD


0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
Lina, if run the script then view the source, copy it and paste it,  I will see why it's not working. It's most probably because you are using the submit button foreach with one form tag so the same variable/value pairs are getting sent to the same script. By looking at the code I can give you javascript code or other method to make it work for you.

As far as posting a new question do what you feel is right but if you do change it copy the link to the new question and post it here so I know to look elsewhere.
0
 

Author Comment

by:lina2401
Comment Utility
As u used its a form problem becasue I'm using 3 submit buttons in 1 form which always transfers me to the URL in ACTION.
Here is my form:
<html>

<head>

<title>Write a Question</title>

</head>

<body>

<FORM METHOD= POST ACTION= "adds.php">

Question

<BR>

<TEXTAREA NAME="Content" COLS="50" ROWS="3">

</TEXTAREA>

<BR>

<BR>

Answer Choices (Select the correct answer)

<BR>

<INPUT NAME="Correct_Answer" TYPE="RADIO" VALUE="0">

<INPUT NAME="Choice0" TYPE="TEXT" SIZE="50">

<BR>

<INPUT NAME="Correct_Answer" TYPE="RADIO" VALUE="1">

<INPUT NAME="Choice1" TYPE="TEXT" SIZE="50">

<BR>

<INPUT NAME="Correct_Answer" TYPE="RADIO" VALUE="2">

<INPUT NAME="Choice2" TYPE="TEXT" SIZE="50">

<BR>

<INPUT NAME="Correct_Answer" TYPE="RADIO" VALUE="3">

<INPUT NAME="Choice3" TYPE="TEXT" SIZE="50">

<BR>

How many more choices do you wish to add

<BR>

<INPUT TYPE="TEXT"SIZE=2 MAXLENGTH=2 NAME="Numb">

<INPUT TYPE="SUBMIT" NAME="Add" VALUE="ADD">

<BR>

<BR>

Image (Optional)

<BR>

<input type="file" name="form_data" size="52">

<BR>

<BR>

Difficulty Level &nbsp;&nbsp;

<SELECT NAME="Difficulty_Level">

<OPTION>1</OPTION>

<OPTION>2</OPTION>

<OPTION>3</OPTION>

</SELECT>

<BR>

<BR>

<INPUT NAME="Private" TYPE="CHECKBOX" VALUE="Y" CHECKED>

Restricted Question (Restricted question can not be used in quick tests)

<BR><BR>

<INPUT TYPE="SUBMIT" NAME="preview" VALUE="Preview Question">

<BR>

<BR>

<INPUT TYPE="RESET" NAME="reset" VALUE="CLEAR">&nbsp;&nbsp;

<INPUT TYPE="SUBMIT" NAME="submit" VALUE="DONE">

</FORM>

</body>

</html>

One more thing when the user wants to edit the question (after previewing it) the value that have been entered should show up again how this can be done for those types of inputs : File, Checklist, Checkbox?

Thanks for ur help.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Expert Comment

by:andriv
Comment Utility
There is a couple ways to have it execute the script you want. One way to use seperate forms surrounding the values you want to send with the appropriate form tags. However, they cannot overlap or it will cause problmes.  The way I handled it was to put all the scripts with the same .php file and added a hidden field without a value, the value was set with javascript using the onClick handler. Or you can have seperate .php files and submit to another .php files that will check for a condition and include the .php file that needs to be executed.

I will explain the later method. First add the javascript to your form:

1) somewhere before the first submit button add:
<input type=hidden name=action value="">

3) add 'name' attribute to form tag:

<FORM NAME="myform" METHOD= POST ACTION= "adds.php">

2) Change the submit type for the first two button to type=button (because NS doesn't support onClick on the type=submit) plus add an onClick event assigning value to the hidden field and to submit form.

<INPUT TYPE="BUTTON" NAME="Add" VALUE="ADD" onClick="document.myform.action.value='add'; document.myform.submit();">

<INPUT TYPE="BUTTON" NAME="preview" VALUE="Preview Question" onClick="document.myform.action.value='preview'; document.myform.submit();">

3) Add the Submit to the form tag that does the same thing as the onClick but only activated if the last SUBMIT button is clicked.

<FORM NAME="myform" METHOD= POST ACTION= "adds.php"
onSubmit="document.myform.action.value='done'">

4) Create a new php file and set the action to the above form tag to point to that new php file. That file will look like what follows:

<?
if($action == 'add')
{
include("add.php");
}
elseif($action == 'preview')
{
include("preview.php");
}
elseif($action == 'done')
{
include("final.php");
}

//Sub the file names above with the correct files
?>

Also make sure that everything in those files are within the PHP tags.

To display the provided information to the form when displaying use the value attribute in the input tag with the PHP variable:

<input type="text name="myname" value="<? print("$myname") ?>">

Checkboxes, radio, and selects are different

Select Box:

<select name="myselect>
  <option value="<? print($myselect) ?>"><? print($myselect) ?></option>
  <option value="one">one</option>
   ...
</select>

Checkbox:

<input type=checkbox name=mycheckbox value= <? (!$mycheckbox)? print(""): print("checked"); ?>>


If you really like PHP and want to learn it well you should take a look at www.getontheworldwideweb.com.  It is worth the money. I have taken several course with them plus I am one of the instructors.
0
 

Author Comment

by:lina2401
Comment Utility
This sounds dummy "I know"!!
How can I add javascript to the form??and what does this mean "document.myform.action.value='add'; document.myform.submit();"??
0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
No it's not a dumb question.  People are not born with all these languages, in fact we can't even speak english when we are born.

There is no need to add any further javascript to the page.  The syntax that you see document.my... is the javascript.  When I first started writing I was going to add the <script> tags to put javascript but I decided on a different method and forgot to remove that sentence.

document.myform.action.value = 'add' is javascript hierarchy to set the value for the hidden field named 'action'.  If you look at the code I had you create a hidden field without a value:

<input type=hidden name=action value="">

The code will give the hidden field a value when you onClick of the button:

onClick="document.myform.action.value ='add'

Here the break down of that line:

onClick of the button go to the 'document' find the form named 'myform' then find the element named 'action' and give it the 'value' of 'add'.  This way the value of that variable 'action' will depend on which button they clicked.   Then when it's sent to the next script you can determin which button was selected by checking the value of 'action' and run the appropriate script on the result:

If the selected 'add' then action will equal 'add' and you can call that script to be executed, if not and they selected 'preview' then you can run the script to preview the question and so on.

Andy
0
 

Author Comment

by:lina2401
Comment Utility
I tried the following:
<html>
<head>
        <title>Write a Question</title>
</head>

<body>
<FORM NAME= "MCQForm" METHOD= POST ACTION= "ProcessForm.php" onSubmit="document.MCQForm.action.value='submit'">
Question
<BR>
<TEXTAREA NAME="Content" COLS="50" ROWS="3">
</TEXTAREA>
<BR>
<BR>
Answer Choices (Select the correct answer)
<BR>
<?
 for ($j=0; $j<4; $j++)
 {
?>
<INPUT NAME="Correct_Answer" TYPE="RADIO" VALUE="<? echo $j; ?>">
<INPUT NAME="Choice[]" TYPE="TEXT" SIZE="50"> <BR>
<?
 }
?>
<INPUT TYPE="HIDDEN" NAME="action" VALUE="">
How many more choices do you wish to add
<BR>
<INPUT TYPE="TEXT"SIZE=2 MAXLENGTH=2 NAME="Numb">
<INPUT TYPE="BUTTON" NAME="Add" VALUE="ADD" onClick="document.MCQForm.action.value='add';document.MCQForm.submit();">
<BR>
<BR>
Image (Optional)
<BR>
<input type="file" name="form_data"  size="50">
<BR>
<BR>
Difficulty Level &nbsp;&nbsp;
<SELECT NAME="Difficulty_Level">
<OPTION>1</OPTION>
<OPTION VALUE="2" SELECTED>2</OPTION>
<OPTION>3</OPTION>
</SELECT>
<BR>
<BR>
<INPUT NAME="Private" TYPE="CHECKBOX" VALUE="Y" CHECKED>
 Restrict Question (Restricted question can not be used in quick tests)
<BR><BR>
<INPUT TYPE="BUTTON" NAME="preview" VALUE="Preview Question" OnClick="document.MCQForm.action.value='preview';document.MCQForm.submit();">
<BR>
<BR>
<INPUT TYPE="RESET" NAME="reset" VALUE="CLEAR">&nbsp;&nbsp;
<INPUT TYPE="SUBMIT" NAME="submit" VALUE="DONE">
</FORM>
</body>
</html>

I got this error whenever I tried to click on the add and preview buttons(object doesn't support this property or method)!!
btw the submit button is working perfectly.
SO what's wrong with the code above??
0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
What line number was the error?
0
 

Author Comment

by:lina2401
Comment Utility
Line 28: <INPUT TYPE="TEXT"SIZE=2 MAXLENGTH=2 NAME="Numb">

Line 47: <BR><BR>

The lines above the buttons.
0
 
LVL 5

Accepted Solution

by:
andriv earned 200 total points
Comment Utility
I just spent an hour trying to figure out the problem. It works for my form but I couldn't get it to work for yours. Then a bell rang in my head. We used <INPUT TYPE="SUBMIT" NAME="submit" VALUE="DONE">.  'submit' is a reserved word so we cannot use it to name anything so the NAME="submit" was causing a conflict.  Change it so the 's' is capital:

<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="DONE"> and it will work.
0
 

Author Comment

by:lina2401
Comment Utility
Awesome Its working!!
Thanks Andy.
0
 
LVL 5

Expert Comment

by:andriv
Comment Utility
No problem
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now