Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

add radiobuttons php form

Featured Replies

Added radiobuttons to form below code , what I added is in red ;

As it is submitted it does not pass the else if(trim($sexo) == '') test ; probably because it does not received

the click info.

Would you please help me out to find what is wrong .

Thanks

 

 

 

 

<!-- The contact form starts from here-->

<?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">Please select sex option</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);

}

 

 

$to = "johndoe@nowhere.com";

 

 

 

$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> 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;?>" />

 

<label><span class="required">*</span> Feminino :</label>

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

 

.........................................................

IS THIS CORRECT : value="<?=$masculino;?>" ????????

......................................................

 

 

 

 

 

<label><span class="required">*</span> Email: </label>

<input name="email" type="text" class="box" id="email" size="30" value="<?=$email;?>" />

 

 

 

<label><span class="required">*</span> Assunto: </label>

<input name="subject" type="text" class="box" id="subject" size="30" value="<?=$subject;?>" />

 

 

 

<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

}

 

function isEmail($email)

{

return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i"

,$email));

}

?>

<!-- END CONTACT FORM -->

 

 

</div> <!-- /contentForm -->

Edited by ronwolpa

  • Author

for 1 the variable $sexo has no $_POST associated with it like this $sexo = $_POST['something'];

Below a summary on what I did (text in red ) ;

Yet , it does not submit the value of radiobutton;

Thank you for your answer.

 

 

<?php

$error = ''; // error message

$name = ''; // sender's name<br />

$sexo = '';

$masculino = '';

$feminino = '';

$email = ''; // sender's email address

$subject = ''; // subject

$message = ''; // the message itself

$spamcheck = ''; // Spam check

 

if(isset($_POST['send']))

{

$name = $_POST['name'];

$sexo = $_POST['sexo']; I ADDED $sexo INSIDE THE IF

$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>';

 

} N O T Q U I T E S U R E A B O U T T H A T

............................................

 

$msg = "From : $name \r\nMasculino: $masculino \r\nFeminino: $feminino \r\nEmail : $email \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";

 

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

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

S O W H A T I S W R O N G ?

 

 

  • Author

I tried to replace radiobuttons by <select> ,

The value of select is submitted empty

 

<select name="sexo" id="sexo" value="<?=$sexo;?>" />

<option value="value">( Selecione )</option>

<option>Masculino</option>

<option>Feminino</option>

</select>

 

Pasted below the received E-mail :

From : Sender

Masculino:

Feminino:

Email : sender@server.net

Subject : [Formulário de contato] : Subject

 

Message :

Message anything...

Edited by ronwolpa

These need to be given a value before hand, like this, not the post variable of the 2 fields

 

<input class="box" type="radio" name="sexo" id="masculino" value="masculino" />
<input class="box" type="radio" name="sexo" id="feminino" value="feminino" />

 

then u echo the post variables in your email

 

something like this

 

$body = "
Gender:$sexo
";

 

along with your other details you want in the email

  • Author

I did exactly like this

<input class="box" type="radio" name="sexo" id="masculino" value="masculino" />
<input class="box" type="radio" name="sexo" id="feminino" value="feminino" />

 

(SIC) then u echo the post variables in your email>> >>>

 

The variable $msg stores the info on this script:

$msg = "From : $name \r\nMasculino: $masculino \r\nFeminino: $feminino \r\nEmail : $email \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";

So I placed inside $ms

With the same result , the email comes in with fields both empty.

Edited by ronwolpa

Um you need to be echoing this variable inside your email $sexo = $_POST['sexo']; since sexo is the name of your radio buttons, then it will get the value of whichever radio button is clicked, and send that value once your forms submitted

  • Author

it works :

$msg = "From : $name \r\nSexo : $sexo \r\nEmail : $email \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";

 

The other problem it happens with the form validation which does not work correctly :

 

if(trim($name) == '')

{

$error = '<div class="errormsg">Complete your name</div>';

}

 

In order to test the form , before filling the fields , I try to submit empty to check the validation. Empty as I click submit with form empty , it jumps the error message direct to subject.

The original script , before adding more variables and form elements , worked fine.

I thought it was just add more elements , respecting the logic structure of the script , to make it work.

 

 

 

 

 

 

 

thank you very much for your support

Edited by ronwolpa

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.