homepage Welcome to WebmasterWorld Guest from 54.211.190.232
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
how to add checkboxes to php form
how to add checkboxes array into if(isset.......
mmKt




msg:4461054
 2:30 am on Jun 4, 2012 (gmt 0)

My 1st post here :
With great difficulty I am developing a form;
I have already the input text fields now I need to add radiobuttons :
<input type="checkbox" name="chkbxarray[]" value="Male">Male
<input type="checkbox" name="chkbxarray[]" value="Female">Female

As I do not know how to do this I need some guidance to add checkbox array to the code , possibly inside the if(isset.... ;
Please help me out.
Thanks

THE CODE I HAVE ALREADY WORKING

<?php
$error = ''; // error message
$name = ''; // sender's name
$email = ''; // sender's email address
$subject = ''; // subject
$message = ''; // the message itself
$spamcheck = ''; // Spam check

if(isset($_POST['send']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$spamcheck = $_POST['spamcheck'];

if(trim($name) == '')
{
$error = '<div class="errormsg">Por favor complete seu nome</div>';
}
else if(trim($email) == '')
{
$error = '<div class="errormsg">Por favor complete seu nome digite seu e-mail</div>';
}
else if(!isEmail($email))
{
$error = '<div class="errormsg">Voce nao digitou um e-mail válido , tente novamente.</div>';
}
if(trim($subject) == '')
{
$error = '<div class="errormsg">Por favor preencha o campo assunto.</div>';
}
else if(trim($message) == '')
{
$error = '<div class="errormsg">Por favor digite sua mensagem.</div>';
}
else if(trim($spamcheck) == '')
{
$error = '<div class="errormsg">Por favor digite o anti-spam</div>';
}
else if(trim($spamcheck) != '14')
{
$error = '<div class="errormsg">Anti-spam: o número digitado nao está correto.</div>';
}
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}

// the email will be sent here
// make sure to change this to be your e-mail
$to2 = "johndoe@nobody.com";
$to = "bogieman@jackfrost.com";

// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change it as you want

$subject = '[Formulário de contato] : ' . $subject;

// the mail message ( add any additional information if you want )
$msg = "From : $name \r\ne-Mail : $email \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";

mail($to2, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");

 

incrediBILL




msg:4461112
 6:33 am on Jun 4, 2012 (gmt 0)

I have already the input text fields now I need to add radiobuttons :


Your example needs radio buttons but you say checkboxes everywhere else, including your code.

If you want radio buttons I think starting using radio buttons would work better than checkboxes:

<label><input class="radio" name="sexytime" value="Mail" checked="checked" type="radio"> Macho</label><br>
<label><input class="radio" name="sexytime" value="Femail" type="radio">Woman</label>

No need for an array, it's just a single value.

mmKt




msg:4461197
 11:44 am on Jun 4, 2012 (gmt 0)

Usually with radiobutton the value carries the info , whether is male or female .
But on this script the value is taken by :
value="<?=$sexo;?>" />
So how I am going to pass the clicked value ?

<label><span class="required">*</span> Masculino :</label>
<input class="box" type="radio" name="sexo" id="sexo" value="<?=$sexo;?>" />

<label><span class="required">*</span> Feminino :</label>
<input class="box" type="radio" name="sexo" id="sexo" value="<?=$sexo;?>" />

mmKt




msg:4461247
 3:15 pm on Jun 4, 2012 (gmt 0)

Hello
I´ve just added the radiobuttons on the code ;
There may be something wrong as the form is submitted it does not pass the else if(trim($sexo) == '') ; perhaps because it does not receive the radiobutton click information.



<?php
$error = ''; // error message
$name = ''; // sender's name
$sexo = ''; // radiobutton sex
$email = ''; // sender's email address
$subject = ''; // subject
$message = ''; // the message itself
$spamcheck = ''; // Spam check

if(isset($_POST['send']))
{
$name = $_POST['name'];
$masculino = $_POST['masculino'];
$feminino = $_POST['feminino'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$spamcheck = $_POST['spamcheck'];

if(trim($name) == '')
{
$error = '<div class="errormsg">Por favor complete seu nome</div>';
}

else if(trim($sexo) == '')
{
$error = '<div class="errormsg">Por favor escolha a opção sexo</div>';

}


else if(trim($email) == '')
{
$error = '<div class="errormsg">Por favor complete seu nome digite seu e-mail</div>';
}


else if(!isEmail($email))
{
$error = '<div class="errormsg">Voce nao digitou um e-mail válido , tente novamente.</div>';
}
if(trim($subject) == '')
{
$error = '<div class="errormsg">Por favor preencha o campo assunto.</div>';
}
else if(trim($message) == '')
{
$error = '<div class="errormsg">Por favor digite sua mensagem.</div>';
}
else if(trim($spamcheck) == '')
{
$error = '<div class="errormsg">Por favor digite o anti-spam</div>';
}
else if(trim($spamcheck) != '14')
{
$error = '<div class="errormsg">Anti-spam: o número digitado nao está correto.</div>';
}
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}

// the email will be sent here
// make sure to change this to be your e-mail
$to = "vazzolla@mandic.com.br";

// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change it as you want

$subject = '[Formulário de contato] : ' . $subject;

// the mail message ( add any additional information if you want )
$msg = "From : $name \r\nSexo: $sexo \r\nEmail : $email \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";

mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");


?>

<!-- Message -->
<div class="goodbye">
<h2>Em breve entraremos em contato</h2>
<p>Muito obrigado <b><?=$name;?></b>&nbsp;por contactar-nos.</p>
</div>
<!--End Message Sent-->


<?php
}
}

if(!isset($_POST['send']) || $error != '')
{
?>

<img src="../images/contato2.png" />
<p class="line-1"> </p>

<!--<h2>Preencha todos os campos:</h2>-->
<!--Error Message-->
<?=$error;?>


<form method="POST" name="form" id="form" class="form-1" action="">
<!-- ORIGINAL <form method="post" name="contFrm" id="contFrm" action=""> -->

<label><span class="required">*</span> Nome :</label>
<input name="name" type="text" class="box" id="name" size="30" value="<?=$name;?>" />
<p class="line-1"> </p>



<label><span class="required">*</span> Masculino :</label>
<input class="box" type="radio" name="sexo" id="sexo" value="<?=$masculino;?>" />
<p class="line-1"> </p>

<label><span class="required">*</span> Feminino :</label>
<input class="box" type="radio" name="sexo" id="sexo" value="<?=$feminino;?>" />
<p class="line-1"> </p>



<label><span class="required">*</span> Email: </label>
<input name="email" type="text" class="box" id="email" size="30" value="<?=$email;?>" />
<p class="line-1"> </p>


<label><span class="required">*</span> Assunto: </label>
<input name="subject" type="text" class="box" id="subject" size="30" value="<?=$subject;?>" />
<p class="line-1"> </p>


<label><span class="required">*</span> Mensagem : </label>
<textarea name="message" cols="40" rows="3" id="message"><?=$message;?></textarea>
<p class="line-1"> </p>


<label><span class="required">*</span> Anti-spam : <b>7 + 7 =</b></label>
<input name="spamcheck" type="text" class="box" id="spamcheck" size="4" value="<?=$spamcheck;?>" /><br /><br />
<p class="line-1"> </p>


<!-- Submit Button-->
<input name="send" type="submit" class="button" id="send" value="" />
</form>

<p class="line-1"> </p>


<!-- E-mail verification. Do not edit -->
<?php

mmKt




msg:4461248
 3:17 pm on Jun 4, 2012 (gmt 0)

Hello
I´ve just added the radiobuttons on the code ;
There may be something wrong as the form is submitted it does not pass the else if(trim($sexo) == '') ; perhaps because it does not receive the radiobutton click information.



<?php
$error = ''; // error message
$name = ''; // sender's name
$sexo = ''; // radiobutton sex
$email = ''; // sender's email address
$subject = ''; // subject
$message = ''; // the message itself
$spamcheck = ''; // Spam check

if(isset($_POST['send']))
{
$name = $_POST['name'];
$masculino = $_POST['masculino'];
$feminino = $_POST['feminino'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$spamcheck = $_POST['spamcheck'];

if(trim($name) == '')
{
$error = '<div class="errormsg">Por favor complete seu nome</div>';
}

else if(trim($sexo) == '')
{
$error = '<div class="errormsg">Por favor escolha a opção sexo</div>';

}


else if(trim($email) == '')
{
$error = '<div class="errormsg">Por favor complete seu nome digite seu e-mail</div>';
}


else if(!isEmail($email))
{
$error = '<div class="errormsg">Voce nao digitou um e-mail válido , tente novamente.</div>';
}
if(trim($subject) == '')
{
$error = '<div class="errormsg">Por favor preencha o campo assunto.</div>';
}
else if(trim($message) == '')
{
$error = '<div class="errormsg">Por favor digite sua mensagem.</div>';
}
else if(trim($spamcheck) == '')
{
$error = '<div class="errormsg">Por favor digite o anti-spam</div>';
}
else if(trim($spamcheck) != '14')
{
$error = '<div class="errormsg">Anti-spam: o número digitado nao está correto.</div>';
}
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}

// the email will be sent here
// make sure to change this to be your e-mail
$to = "johndoe@nowhere.com";

// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change it as you want

$subject = '[Formulário de contato] : ' . $subject;

// the mail message ( add any additional information if you want )
$msg = "From : $name \r\nSexo: $sexo \r\nEmail : $email \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";

mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");


?>

<!-- Message -->
<div class="goodbye">
<h2>Em breve entraremos em contato</h2>
<p>Muito obrigado <b><?=$name;?></b>&nbsp;por contactar-nos.</p>
</div>
<!--End Message Sent-->


<?php
}
}

if(!isset($_POST['send']) || $error != '')
{
?>

<img src="../images/contato2.png" />
<p class="line-1"> </p>

<!--<h2>Preencha todos os campos:</h2>-->
<!--Error Message-->
<?=$error;?>


<form method="POST" name="form" id="form" class="form-1" action="">
<!-- ORIGINAL <form method="post" name="contFrm" id="contFrm" action=""> -->

<label><span class="required">*</span> Nome :</label>
<input name="name" type="text" class="box" id="name" size="30" value="<?=$name;?>" />
<p class="line-1"> </p>



<label><span class="required">*</span> Masculino :</label>
<input class="box" type="radio" name="sexo" id="sexo" value="<?=$masculino;?>" />
<p class="line-1"> </p>

<label><span class="required">*</span> Feminino :</label>
<input class="box" type="radio" name="sexo" id="sexo" value="<?=$feminino;?>" />
<p class="line-1"> </p>



<label><span class="required">*</span> Email: </label>
<input name="email" type="text" class="box" id="email" size="30" value="<?=$email;?>" />
<p class="line-1"> </p>


<label><span class="required">*</span> Assunto: </label>
<input name="subject" type="text" class="box" id="subject" size="30" value="<?=$subject;?>" />
<p class="line-1"> </p>


<label><span class="required">*</span> Mensagem : </label>
<textarea name="message" cols="40" rows="3" id="message"><?=$message;?></textarea>
<p class="line-1"> </p>


<label><span class="required">*</span> Anti-spam : <b>7 + 7 =</b></label>
<input name="spamcheck" type="text" class="box" id="spamcheck" size="4" value="<?=$spamcheck;?>" /><br /><br />
<p class="line-1"> </p>


<!-- Submit Button-->
<input name="send" type="submit" class="button" id="send" value="" />
</form>

<p class="line-1"> </p>


<!-- E-mail verification. Do not edit -->
<?php

rocknbil




msg:4461273
 4:31 pm on Jun 4, 2012 (gmt 0)

Look:

<input class="box" type="radio" name="sexo" id="sexo" value="<?=$masculino;?>" />

Then

if(isset($_POST['send']))
{
$masculino = $_POST['masculino'];
$feminino = $_POST['feminino'];
}

You don't WANT to modify these values for the radio, and they won't work anyway - you have them pointed at the wrong post field name, it should be sexo. (there IS no $_POST['masculino'].) Remove those two lines and look at the below. (Also what if they press ENTER and don't use the SEND button?)

View the source code of your page before submitting, and you'll see the radio field values for sexo are empty and will stay that way because you're submitting them empty.

Unrelated, you have an HTML error. ID's are supposed to be unique so you can't use "sexo" to ID all elements. This will mess up any Javascript validation you have.

Another caveat: radio buttons and checkboxes work they way they work to make our job easier. A radio button is supposed to have one checked by default.

This is still not right but shown to demonstrate the previous statements:

<p><label><span class="required">*</span> Prefiero no Decir :</label>
<input class="box" type="radio" name="sexo" id="sexo-none" value="Prefiero no Decir" checked="checked" /></p>

<p><label><span class="required">*</span> Masculino :</label>
<input class="box" type="radio" name="sexo" id="sexo-masculino" value="masculino" /></p>

<p><label><span class="required">*</span> Feminino :</label>
<input class="box" type="radio" name="sexo" id="sexo-feminino" value="feminino" /></p>

Last and probably most important: checkboxes or radios will only show up in $_POST or $_GET if they are checked. So you only need to check if they are set.

if (isset($_POST['some-checkbox-field-name'])) { echo 'This box was checked.'; }


As mentioned, remove the two lines shown, and do something like this. This is not copy and paste code and shown for example only.

<p><label><span class="required">*</span> Prefiero no Decir :</label>
<input class="box" type="radio" name="sexo" id="sexo-none" value="Prefiero no Decir"
<?php if (! isset($_POST['sexo']) or (isset($_POST['sexo']) and ($_POST['sexo']=='Prefiero no Decir'))) { echo 'checked="checked"'; } ?> /></p>

<p><label><span class="required">*</span> Masculino :</label>
<input class="box" type="radio" name="sexo" id="sexo-masculino" value="masculino"
<?php if (isset($_POST['sexo']) and ($_POST['sexo']=='masculino')) { echo 'checked="checked"'; } ?>/></p>

<p><label><span class="required">*</span> Feminino :</label>
<input class="box" type="radio" name="sexo" id="sexo-feminino" value="feminino"
<?php if (isset($_POST['sexo']) and ($_POST['sexo']=='feminino')) { echo 'checked="checked"'; } ?>/></p>

That will stabilize your values from the form to the submit.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved