Page 1 of 1

html value access

Posted: Thu Jun 26, 2008 10:04 am
by akaballa
hi,

I am a neb with php coding, but would definitely like to learn! I am creating a simple website using php and a back frame mysql database. I have created a form with several fields using html. my question is....I want to create a case where if a user chooses option A from a dropdown box then an extra input textbox must appear instantaneosly for the user to fill.

For example. Say I have a add training from. One of the fields asks the user if the new training is internal. If the user answers no, then a new organization text field must appear immediately ( prior to submission) for the user to fill in.

this is the code I tried:

<html>
<body>
<form action = "adddTrainingTbl.php" method="post">
TrainingName
<input type="text" name = "tName">
<br>
<br>
Internal Training?
<select name="iTraining">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<br>
<?php

if ($_REQUEST['itraining'] == 'No')
{
echo "Organization".'<br>'.'<br>'.'<input type="text" name="organization">';
}
?>
<br>
<br>
Cost
<input type="text" name="cost">
<br>
<br>
<input type="submit" value="submit">

</form>
</body>
</html>

I probably know that I shoudnt be using $_REQUEST if I am not submittting the form. However, i had to try something :). Can someone please assist? Thanks!!

Re: html value access

Posted: Thu Jun 26, 2008 10:40 am
by Reviresco
You want to use javascript with CSS for that. Put the "organization" input in a div, set the div's display style to "none", and give the div an id. When the select input is changed, if its value is "no", the div's display style is changed to "block". If "yes" is selected, it changes back to "none".

(I also changed the double <br /> tags to <p> paragraphs.)

Code: Select all

<html>
<body>
 
<form action = "adddTrainingTbl.php" method="post">
TrainingName
<input type="text" name = "tName">
<p>
Internal Training?
<select name="iTraining" onchange="if (this.value == 'no') { document.getElementById('organization').style.display = 'block';} else{ document.getElementById('organization').style.display = 'none';} ">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
</p>
<div id="organization" style="display:none;">
<p>
Organization <input type="text" name="organization">
</p>
</div>
<p>
Cost <input type="text" name="cost">
</p>
<p>
<input type="submit" value="submit">
</p>
</form>
 
</body>
</html>

Re: html value access

Posted: Thu Jun 26, 2008 12:39 pm
by akaballa
hey..thanks a lot! I never knew we had to implement javascript/css to work with blocks. I always thought php itself was capable of handling it. Thnks a bunch though! I just have one more question if you dont mind. After I submit the form, the form gets submitted (POST) to a new page, where i have specified instructions on how to insert the information to the database. I wrote the logic code to insert the information ( i had to consider the itraining option chosen,etc.). While testing my submission, I receive this error:

Error: Query was empty

This is what I wrote on the second file that accepts the information from the form:

include("mysqlConnection.php");

if ( $_REQUEST['itraining'] == "no" ) //have a feeling this is where the problem is
{
$insert = "INSERT INTO addtrainingtbl
(trainingName, internal, external, organization, classType, cost)
VALUES('$_REQUEST[tName]','$_REQUEST[iTraining]',
yes , '$_REQUEST[organization]','$_REQUEST[classType]','$_REQUEST[cost]')";
}
elseif ( $_REQUEST['itraining'] =="yes" ) //have a feeling this is where the problem is
{
$insert = "INSERT INTO addtrainingtbl
(trainingName, internal, external, organization, cost)
VALUES('$_REQUEST[tName]','$_REQUEST[iTraining]',
no , null ,'$_REQUEST[classType]','$_REQUEST[cost]')";
}

if (!mysql_query($insert,$con))

{
die('Error: ' . mysql_error());
}

else
{
echo "Thanks for submitting!";
// echo "Thank you"<br><a href="addSession.php">to send another form go here.</a>";
}

Thanks!

Re: html value access

Posted: Thu Jun 26, 2008 3:59 pm
by Reviresco
At first glance, it seems like the problem might be that the words "yes" and "no" in the "values" section should be in quotes -- although I'm not sure if that would be causing the empty query error.