how to create a check vote box poll with php scripts and html ???
plx someone help me!!

Dani AI

Generated

Good starting points from and . For a checkbox poll that emails results and caps selections at five, you want two layers of protection: (1) client-side UX to prevent choosing more than 5, and (2) server-side validation so the limit cannot be bypassed. Also whitelist the allowed options before sending anything to email.

Example form (repeat the label line until you have 12 options). The tiny JS prevents the 6th check:

<form method="post" action="">
  <p>Pick up to 5:</p>
  <label><input type="checkbox" name="choices[]" value="a"> Option A</label>
  <label><input type="checkbox" name="choices[]" value="b"> Option B</label>
  <label><input type="checkbox" name="choices[]" value="c"> Option C</label>
  <!-- ...add 9 more... -->
  <button type="submit">Vote</button>
</form>

<script>
document.addEventListener('change', function (e) {
  if (e.target.name === 'choices[]') {
    const checked = document.querySelectorAll('input[name="choices[]"]:checked');
    if (checked.length > 5) { e.target.checked = false; alert('Select up to 5.'); }
  }
});
</script>

Server-side handler (enforces the limit, whitelists values, and emails). Adjust addresses and option list:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $allowed = ['a','b','c','d','e','f','g','h','i','j','k','l'];
  $choices = isset($_POST['choices']) && is_array($_POST['choices']) ? $_POST['choices'] : [];
  $choices = array_values(array_intersect($choices, $allowed));
  if (count($choices) === 0) { exit('Please select at least one option.'); }
  if (count($choices) > 5)   { exit('Too many selections.'); }

  $to = 'you@example.com';
  $subject = 'New poll vote';
  $body = "Selections:\n- " . implode("\n- ", $choices);
  $headers = "From: no-reply@example.com\r\n";
  mail($to, $subject, $body, $headers);
  echo 'Thanks for voting!';
}

For : to place answers side-by-side in WordPress, wrap them and use flex:

<div style="display:flex; gap:8px; align-items:center;">
  <button>Yey!</button>
  <button>Nay!</button>
  <a class="button" href="#">Click to buy</a>
</div>

Tip: consider storing votes in a database to tally and email summaries, and add a CSRF token to the form.

Recommended Answers

All 4 Replies

You'll probably have to be more specific, and posting your code is what is normally needed.

If you mean, how do you write an HTML with radio buttons and the PHP to read it, I'm going to put it all in one file here to make it easier.


poll.php

<?php
if ($_POST['submit'])
 {
 $value=$_POST['result'];
 echo "You chose $value";
 }
?>

<form action="poll.php" method="post">
Vote for your favorite<br />
<input type="radio" name="result" value="Ford" /> Ford<br />
<input type="radio" name="result" value="Chevy" /> Chevy<br />
<input type="radio" name="result" value="Dodge" /> Dodge<br />
<input type="radio" name="result" value="Toyota" /> Toyota<br />
<input type="radio" name="result" value="Honda" /> Honda<br />
<input type="radio" name="result" value="Nissan" /> Nissan<br />
<input type='submit' name='submit' value='Vote' />
</form>

You can paste this code into a file between the <body> and </body> tags and it should(unless I forgot something) display a list of makes of car, click on Vote and it should show you your choice.

Modify to your hearts content. :)

Also check out: http://www.w3schools.com/html/html_forms.asp

i want to a PHP function that the results should come to my email address.. :) 12 check box will be there and maximum 5 should be selected..!! Actually it is a v simple vote poll which i want to make very soon. any ways thankx for the reply!! I am again hoping for a fast and good favorable solution. Please help me soon coxx i'm just new in HTML and PHP.

Some helpful hints when working with HTML checkboxes and PHP.

1. Name the checkbox entries the same and add brackets to the end of the name. This will result in all the selections being stored into an array when submitted to php.
2. If zero entries are selected, the form field is not passed at all. Your script must test for this.
Figure 1: A simple form full of check boxes of the same name.

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>What are your favorite colors?</p>
<label><input type="checkbox" name="favoriteColor[]" value="red">Red</label>
<label><input type="checkbox" name="favoriteColor[]" value="orange">Orange</label>
<label><input type="checkbox" name="favoriteColor[]" value="yellow">Yellow</label>
<label><input type="checkbox" name="favoriteColor[]" value="green">Green</label>
<label><input type="checkbox" name="favoriteColor[]" value="blue">Blue</label>
<label><input type="checkbox" name="favoriteColor[]" value="indigo">Indigo</label>
<label><input type="checkbox" name="favoriteColor[]" value="violet">Violet</label>
<!-- other form fields here. -->
<input type='submit' name='submit' value='Vote' />
</form>

PHP will treat that as an array called $_POST.

Figure 2: PHP handler snippet for the form in figure 1.

<?php
if(isset($_POST['favoriteColor']) && count($_POST['favoriteColor']))
{
	$validColors = array('red','orange','yellow','green','blue','indigo','violet');
	$countColors = 0;
	foreach($_POST['favoriteColor'] as $color)
	{
		if(in_array($color, $validColors)) {
			$countColors++;
			// do something with received values.
		} else {
			echo "The color '$color' is not a valid option.  Ignoring.";
		}
	}
	if($countColors > 0) {
		// send email or something when done.
	}
}
?>

Hello guys,
I am looking to create a poll in WordPress using HTML code.
I found many usufull into on the web, but i have a problem with undestanding how to put answer keys side-by-side?

I wamned it to look like this :

Far Cry 5

Yey! (button) Nay! (button) .. if possible Click to buy (button)

Thankank you a ton!
Nataie

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.