#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    16
    Rep Power
    0

    OnClick function not working when adding hidden field


    Hello:
    I have a checkbox (AV1) and a textbox (AV1Fee) with the following function:

    function valueAV1Fee(formobj) {
    if (document.forms[0].AV1.checked == true) {
    document.forms[0].AV1Fee.value = 600;
    }
    else {
    document.forms[0].AV1Fee.value = 0;
    }

    Here is the HTML:
    <input type="checkbox" name="AV1" value="Yes" onClick="javascript:valueAV1Fee(this);">

    So when AV1 is checked, AV1Fee becomes 600 and AV1's value is "Yes". I'm trying to set the default value of AV1 to print out "No" instead of blank in the database field. This works for that:

    <input type="hidden" name="AV1" value="No">
    <input type="checkbox" name="AV1" value="Yes" onClick="javascript:valueAV1Fee(this);">

    So when AV1 is not checked, the value in the database field becomes "No". However, the problem is by adding this hidden field, the onClick function no longer works and nothing happens with AV1Fee when AV1 is checked.

    Any clues about how to make it work or how to set the unchecked value of AV1 to "No"?

    Thank you.
    cdr6800
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    24
    Rep Power
    0
    Remove that hidden field.
    It's easier to solve this on the back-end.

    In what language is the form processed ?

    If it's PHP you simple can do this :

    PHP Code:
    if( !isset($_POST[AV1]) )
    {
    // Run code when the checkbox isn't set.

    $_POST[AV1] = 'No';


    Léon
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    16
    Rep Power
    0
    Thanks Leon..the file that processes the form is php, but I couldn't get that to work. Maybe I'm not putting it in the right place? Could you tell me where you think it should go? I am pasting the code below:

    <?php
    require_once("scripts/access.class.php");
    require_once("config.php");

    //Receiving variables
    $Date = date("m/d/Y");
    $Username=$_POST['Username'];
    $Password=$_POST['Password'];
    $Hint=$_POST['Hint'];
    $MeetingTitle=$_POST['MeetingTitle'];
    $Choice1MeetingTime=$_POST['Choice1MeetingTime'];
    $Choice2MeetingTime=$_POST['Choice2MeetingTime'];
    $Choice3MeetingTime=$_POST['Choice3MeetingTime'];
    $MeetingLength=$_POST['MeetingLength'];
    $NumberAttendees=$_POST['NumberAttendees'];
    $OpenOrClosed=$_POST['OpenOrClosed'];
    $Moderator1Name=$_POST['Moderator1Name'];
    $Moderator1Phone=$_POST['Moderator1Phone'];
    $Moderator1Organization=$_POST['Moderator1Organization'];
    $Moderator1OtherOrganization=$_POST['Moderator1OtherOrganization'];
    $Moderator1Email=$_POST['Moderator1Email'];
    $Moderator2Name=$_POST['Moderator2Name'];
    $Moderator2Phone=$_POST['Moderator2Phone'];
    $Moderator2Organization=$_POST['Moderator2Organization'];
    $Moderator2OtherOrganization=$_POST['Moderator2OtherOrganization'];
    $Moderator2Email=$_POST['Moderator2Email'];
    $MeetingCoordinatorName=$_POST['MeetingCoordinatorName'];
    $MeetingCoordinatorPhone=$_POST['MeetingCoordinatorPhone'];
    $MeetingCoordinatorOrganization=$_POST['MeetingCoordinatorOrganization'];
    $MeetingCoordinatorOtherOrganization=$_POST['MeetingCoordinatorOtherOrganization'];
    $MeetingCoordinatorEmail=$_POST['MeetingCoordinatorEmail'];
    $CompetingMeeting1=$_POST['CompetingMeeting1'];
    $CompetingMeeting2=$_POST['CompetingMeeting2'];
    $CompetingMeeting3=$_POST['CompetingMeeting3'];
    $SetupChoice1=$_POST['SetupChoice1'];
    $SetupChoice2=$_POST['SetupChoice2'];
    $AV1Fee=$_POST['AV1Fee'];
    $AV2=$_POST['AV2'];
    $AV2Fee=$_POST['AV2Fee'];
    $AV3=$_POST['AV3'];
    $AV3Fee=$_POST['AV3Fee'];
    $AV4=$_POST['AV4'];
    $AV4Fee=$_POST['AV4Fee'];
    $AV5=$_POST['AV5'];
    $AV5Fee=$_POST['AV5Fee'];
    $AV6=$_POST['AV6'];
    $AV6Fee=$_POST['AV6Fee'];
    $AV7=$_POST['AV7'];
    $AV7Fee=$_POST['AV7Fee'];
    $AV8=$_POST['AV8'];
    $AV8Fee=$_POST['AV8Fee'];
    $AV9=$_POST['AV9'];
    $AV9Fee=$_POST['AV9Fee'];
    $AV10=$_POST['AV10'];
    $AV10Fee=$_POST['AV10Fee'];
    $AV11=$_POST['AV11'];
    $AV11Fee=$_POST['AV11Fee'];
    $AV12=$_POST['AV12'];
    $AV12Fee=$_POST['AV12Fee'];
    $AV13=$_POST['AV13'];
    $AV13Fee=$_POST['AV13Fee'];
    $AVTotalFee=$_POST['AVTotalFee'];
    $AVScholarship=$_POST['AVScholarship'];
    $OrderPolycom=$_POST['OrderPolycom'];
    $OrderWiFi=$_POST['OrderWiFi'];
    $SendCateringMenus=$_POST['SendCateringMenus'];
    $CateringContactCoordinator=$_POST['CateringContactCoordinator'];

    // First create the user with the access class
    // This handles proper password generation
    $link = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    @mysql_select_db(DB_NAME) or die( "Unable to select database");

    $access = new flexibleAccess($link);
    $userId = $access->insertUser(
    array(
    'MeetingNumber' => 'null',
    'Date' => $Date,
    'Username' => $Username,
    'Password' => $Password,
    'Hint' => $Hint,
    'MeetingTitle' => $MeetingTitle,
    'Choice1MeetingTime' => $Choice1MeetingTime,
    'Choice2MeetingTime' => $Choice2MeetingTime,
    'Choice3MeetingTime' => $Choice3MeetingTime,
    'MeetingLength' => $MeetingLength,
    'NumberAttendees' => $NumberAttendees,
    'OpenOrClosed' => $OpenOrClosed,
    'Moderator1Name' => $Moderator1Name,
    'Moderator1Phone' => $Moderator1Phone,
    'Moderator1Organization' => $Moderator1Organization,
    'Moderator1OtherOrganization' => $Moderator1OtherOrganization,
    'Moderator1Email' => $Moderator1Email,
    'Moderator2Name' => $Moderator2Name,
    'Moderator2Phone' => $Moderator2Phone,
    'Moderator2Organization' => $Moderator2Organization,
    'Moderator2OtherOrganization' => $Moderator2OtherOrganization,
    'Moderator2Email' => $Moderator2Email,
    'MeetingCoordinatorName' => $MeetingCoordinatorName,
    'MeetingCoordinatorPhone' => $MeetingCoordinatorPhone,
    'MeetingCoordinatorOrganization' => $MeetingCoordinatorOrganization,
    'MeetingCoordinatorOtherOrganization' => $MeetingCoordinatorOtherOrganization,
    'MeetingCoordinatorEmail' => $MeetingCoordinatorEmail,
    'CompetingMeeting1' => $CompetingMeeting1,
    'CompetingMeeting2' => $CompetingMeeting2,
    'CompetingMeeting3' => $CompetingMeeting3,
    'SetupChoice1' => $SetupChoice1,
    'SetupChoice2' => $SetupChoice2,
    'AV1' => $AV1,
    'AV1Fee' => $AV1Fee,
    'AV2' => $AV2,
    'AV2Fee' => $AV2Fee,
    'AV3' => $AV3,
    'AV3Fee' => $AV3Fee,
    'AV4' => $AV4,
    'AV4Fee' => $AV4Fee,
    'AV5' => $AV5,
    'AV5Fee' => $AV5Fee,
    'AV6' => $AV6,
    'AV6Fee' => $AV6Fee,
    'AV7' => $AV7,
    'AV7Fee' => $AV7Fee,
    'AV8' => $AV8,
    'AV8Fee' => $AV8Fee,
    'AV9' => $AV9,
    'AV9Fee' => $AV9Fee,
    'AV10' => $AV10,
    'AV10Fee' => $AV10Fee,
    'AV11' => $AV11,
    'AV11Fee' => $AV11Fee,
    'AV12' => $AV12,
    'AV12Fee' => $AV12Fee,
    'AV13' => $AV13,
    'AV13Fee' => $AV13Fee,
    'AVScholarship' => $AVScholarship,
    'OrderPolycom' => $OrderPolycom,
    'OrderWiFi' => $OrderWiFi,
    'SendCateringMenus' => $SendCateringMenus,
    'CateringContactCoordinator' => $CateringContactCoordinator,
    'active' => 1
    )
    );
    $MeetingNumber = sprintf("%04s", mysql_insert_id());
    mysql_close($link);


    //Sending email to HMORN
    $pfw_header = "From: $MeetingCoordinatorEmail\n"
    . "Reply-To: $MeetingCoordinatorEmail\n";
    $pfw_subject = "HMORN Meeting Booked";
    $pfw_email_to = "cdrossen@cdrmarketing.com";
    $pfw_message = "A meeting has been booked on the HMORN website. The meeting coordinator's name is $MeetingCoordinatorName\n"
    . "The meeting number is $MeetingNumber\n"
    . "Your username is $Username and your password is $Password. Your password hint is $Hint\n"
    . "To get the full information for this meeting booking, go to www.hmornmeeting.org.\n";
    @mail($pfw_email_to, $pfw_subject ,$pfw_message ,$pfw_header ) ;

    //send to redirect page after submitting form
    $redirect = "http://www.hmornmeeting.org/redirect_meeting.htm";
    header("Location:" .$redirect);
    ?>
  6. #4
  7. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,161
    Rep Power
    492
    Hi cdr6800
    You need to swap the line that you have:-
    PHP Code:
    $AV1Fee=$_POST['AV1Fee']; 
    With the code that Leon posted.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    24
    Rep Power
    0
    Paste it just below this line and you'll be good :

    $CateringContactCoordinator=$_POST['CateringContactCoordinator'];

    Léon
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    16
    Rep Power
    0
    Sorry guys...I tried putting the code in both places (I actually tried both before my last post) and the field is still coming up blank in the database, without a 'No'. Not sure what the solution is. Any ideas? Thanks for your help.

    cdr6800
  12. #7
  13. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,161
    Rep Power
    492
    Originally Posted by cdr6800
    Sorry guys...I tried putting the code in both places (I actually tried both before my last post) and the field is still coming up blank in the database, without a 'No'. Not sure what the solution is. Any ideas? Thanks for your help.

    cdr6800
    Can you please add a line in your PHP code under the "//Receiving variables" will do ...

    PHP Code:
    echo "<pre>"print_r($_POST); echo "</pre>"
    When you do that, re-submit your form and then it'll give you a load of output to the screen, if you could please re-post that here then we can see what is being sent to the processing php page and can advise a little better.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    16
    Rep Power
    0
    Originally Posted by badger_fruit
    Can you please add a line in your PHP code under the "//Receiving variables" will do ...

    PHP Code:
    echo "<pre>"print_r($_POST); echo "</pre>"
    When you do that, re-submit your form and then it'll give you a load of output to the screen, if you could please re-post that here then we can see what is being sent to the processing php page and can advise a little better.
    badger_fruit:

    Here is the output. This is after I swapped Leon's code with the line you told me to. Thank. cdr6800

    Array
    (
    [DateSubmitted] => 10/15/2012
    [Username] =>
    [Password] =>
    [Hint] =>
    [MeetingTitle] =>
    [Choice1MeetingTime] =>
    [Choice2MeetingTime] =>
    [Choice3MeetingTime] =>
    [Moderator1Name] =>
    [Moderator1Organization] =>
    [Moderator1Phone] =>
    [Moderator1OtherOrganization] =>
    [Moderator1Email] =>
    [Moderator2Name] =>
    [Moderator2Organization] =>
    [Moderator2Phone] =>
    [Moderator2OtherOrganization] =>
    [Moderator2Email] =>
    [MeetingCoordinatorName] =>
    [MeetingCoordinatorOrganization] =>
    [MeetingCoordinatorPhone] =>
    [MeetingCoordinatorOtherOrganization] =>
    [MeetingCoordinatorEmail] => cdr6545@gmail.com
    [CompetingMeeting1] =>
    [CompetingMeeting2] =>
    [CompetingMeeting3] =>
    [SetupChoice1] =>
    [SetupChoice2] =>
    [AV1Fee] => 0
    [AV2Fee] => 0
    [AV3Fee] => 0
    [AV4Fee] => 0
    [AV5Fee] => 0
    [AV6Fee] => 0
    [AV7Fee] => 0
    [AV8Fee] => 0
    [AV9Fee] => 0
    [AV10Fee] => 0
    [AV11Fee] => 0
    [AV12Fee] => 0
    [AV13Fee] => 0
    [AVTotalFee] =>
    [B1] => Submit
    )
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    24
    Rep Power
    0
    PHP Code:
    [AV1Fee] => 
    AV1 is already 0.
    You don't need to change it to no.

    0 = No. ( Not checked )
    1 = Yes. ( Checked )

    Try to check the checkbox and see if the page returns 1.
    If so, you can simply do this:

    PHP Code:
    if($_POST['AV1Fee'])
    {
    // Do stuff if the checkbox is checked.

    }
    else
    {
    // Do stuff if the checkbox isn't checked.

    Léon,
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    16
    Rep Power
    0
    Great solution Leon...never thought of that. I do need it to say "No" rather than 0 so that worked perfectly. Thanks again.

    cdr6800
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    24
    Rep Power
    0
    Originally Posted by cdr6800
    Great solution Leon...never thought of that. I do need it to say "No" rather than 0 so that worked perfectly. Thanks again.

    cdr6800
    You're welcome.
    There's still just one little tip I want to give you.

    Wrap you're data between addslashes() when getting them from a form.
    Some hacker could insert some evil code if you don't.
    It will add slashes before quotes.
    Google for 'mySQL injection' if you want to know more about it.

    PHP Code:
    $AV1Fee=addslashes$_POST['AV1Fee'] ); 
    You can also iterate through the data to make it a little easier and faster:

    PHP Code:
    $saveData = array();
    for(
    $i 1$i <= 13$i++ )
    {
    saveData[$i] = addslashes$_POST['AV'.$i.'Fee'] );

    This will make an array (saveData) wich contains the save data from [AV1Fee] to [AV13Fee].

    saveData[1] = [AV1Fee] and so on ...


    Cheers !

    Comments on this post

    • badger_fruit agrees
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    16
    Rep Power
    0
    Thanks for the great tips. I will definitely use them next time I do a form. As you can tell, I'm pretty new to this.

    cdr6800
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    24
    Rep Power
    0
    Originally Posted by cdr6800
    Thanks for the great tips. I will definitely use them next time I do a form. As you can tell, I'm pretty new to this.

    cdr6800
    You can always send me an email.
    ( leon-designs@hotmail.com )

    The email might seem a little bit misleading,
    I never design anything lol !
    I like programming a lot more.

    Sorry for my bad English btw ,

    Cheers !
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    16
    Rep Power
    0
    Leon:

    I may definitely take you up on that if I need help. I appreciate that. I have your email. I didn't notice any problem with your English by the way.

    David (cdr6800)

IMN logo majestic logo threadwatch logo seochat tools logo