943,169 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 436
  • PHP RSS
Aug 31st, 2010
0

Help with PHP radio buttons

Expand Post »
Hi guys,

My first post here after reading some very helpful responses over the last few months.
I'm having trouble passing radio button values to a results page and wondered if anyone would be able to spot anything wrong with the following code?

Any ideas are very much appreciated as this is the first time I've used PHP forms with radio buttons...

Here's the code section for the form page:
--------
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$interest = $_POST['interest'];
$hear = $_POST['hear'];
$radioq1 = $_POST['radioq1'];
$radioq2 = $_POST['radioq2'];
$radioq3 = $_POST['radioq3'];
$radioq4 = $_POST['radioq4'];
$radioq5 = $_POST['radioq5'];
$radioq6 = $_POST['radioq6'];
$radioq7 = $_POST['radioq7'];
$radioq8 = $_POST['radioq8'];
$radioq9 = $_POST['radioq9'];
$radioq10 = $_POST['radioq10'];
$score = 0;
}
--------
These are the NAMES for the form entries which then get POSTed to another page that just processes:

<?php

$points = $_POST['$score + $radioq1 + $radioq2 + $radioq3 + $radioq4 + $radioq5 + $radioq6 + $radioq7 + $radioq8 + $radioq9 + $radioq10'];

if ($points > 15) {
header('location: results-page.php?id=4');
} else if ($points > 11) {
header('location: results-page.php?id=3');
} else if ($points > 5) {
header('location: results-page.php?id=2');
} else {
header('location: results-page.php?id=1');
}
?>

The final page just shows the page and draws text and images from a database so simple stuff. The only thing is I can't get the values to add up! The result is always the same: results-page?id=1

Iknow it's something simple but my head hurts!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
koldhands is offline Offline
4 posts
since Aug 2010
Aug 31st, 2010
0
Re: Help with PHP radio buttons
Hi koldhands,
Is there any field in your markup with name="submit". If not change the line containing 'isset' to
PHP Syntax (Toggle Plain Text)
  1. isset($_POST['name'])
or
PHP Syntax (Toggle Plain Text)
  1. isset($_POST['email'])
or with name of any other field or simply use:
PHP Syntax (Toggle Plain Text)
  1. if ($_SERVER['REQUEST_METHOD'] == 'POST')
.
Hope this helps...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hari.sarvothama is offline Offline
15 posts
since Aug 2010
Aug 31st, 2010
0
Re: Help with PHP radio buttons
Sorry after posting I realised there's a few things I should have included in the post: the form does have a submit button and the process page is only there to make it easier for me to check eveything works. I'll eventually paste the 2 pages together once I get it working.
Also, just to be a bit clearer, the specific problem is that I can't collect the score values.
Thanks Hari for the response though!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
koldhands is offline Offline
4 posts
since Aug 2010
Sep 4th, 2010
0
Re: Help with PHP radio buttons
Ok here's teh whole code in the hope that someone can possibly see whats going wrong here. About to give up and use javascript but that will probably just bring up more problems!
Any help would be very very much appreciated!!!

Here's the code for the first page:
(I've removed some database stuff but I know half of this page is working as the redirect works fine??)

PHP Syntax (Toggle Plain Text)
  1. <?php require_once('Connections/database.php'); ?>
  2. <?php
  3. $your_email ='my email address';
  4.  
  5. session_start();
  6. $errors = '';
  7. $name = '';
  8. $visitor_email = '';
  9. $phone = '';
  10. $city = '';
  11. $interest = '';
  12. $hear = '';
  13. $newsletter = '';
  14. $score = 0;
  15.  
  16. if(isset($_POST['submit']))
  17. {
  18. $name = $_POST['name'];
  19. $visitor_email = $_POST['email'];
  20. $phone = $_POST['phone'];
  21. $city = $_POST['city'];
  22. $interest = $_POST['interest'];
  23. $hear = $_POST['hear'];
  24. $newsletter = $_POST['newsletter'];
  25.  
  26. for($i=0;$i<11;$i++){
  27. $answer = $_POST['radioq'.$i];
  28. $score += $answer;
  29. }
  30.  
  31. ///------------Do Validations-------------
  32. if(empty($name)||empty($visitor_email)||empty($phone)||empty($city))
  33. {
  34. $errors .= "\n Name, Email, Phone and City are required fields. ";
  35. }
  36. if(IsInjected($visitor_email))
  37. {
  38. $errors .= "\n Bad email value!";
  39. }
  40. if(empty($_SESSION['6_letters_code'] ) ||
  41. strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
  42. {
  43. //Note: the captcha code is compared case insensitively.
  44. //if you want case sensitive match, update the check above to
  45. // strcmp()
  46. $errors .= "\n The captcha code does not match!";
  47. }
  48.  
  49. if(empty($errors))
  50. {
  51. //send the email
  52. $to = $your_email;
  53. $subject="form submission";
  54. $from = $your_email;
  55. $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
  56.  
  57. $body = "A user called $name submitted the form:\n".
  58. "Name: $name\n".
  59. "Email: $visitor_email \n".
  60. "Contact number: $phone\n".
  61. "City: $city\n".
  62. "$score \n".
  63. "\n";
  64.  
  65. $headers = "From: $from \r\n";
  66. $headers .= "Reply-To: $visitor_email \r\n";
  67.  
  68. mail($to, $subject, $body, $headers);
  69.  
  70. if ($score > 15) {
  71. header('location: next-page.php?id=4');
  72. } else if ($score > 11) {
  73. header('location: next-page.php?id=3');
  74. } else if ($score > 5) {
  75. header('location: next-page.php?id=2');
  76. } else {
  77. header('location: next-page.php?id=1');
  78. }
  79. }
  80. }
  81.  
  82. // Function to validate against any email injection attempts
  83. function IsInjected($str)
  84. {
  85. $injections = array('(\n+)',
  86. '(\r+)',
  87. '(\t+)',
  88. '(%0A+)',
  89. '(%0D+)',
  90. '(%08+)',
  91. '(%09+)'
  92. );
  93. $inject = join('|', $injections);
  94. $inject = "/$inject/i";
  95. if(preg_match($inject,$str))
  96. {
  97. return true;
  98. }
  99. else
  100. {
  101. return false;
  102. }
  103. }
  104. ?>

And the code on the second page which should show the result:

<h1>Here are your results</h1>
<span style="font-size:3em;"><?php echo $_POST['score']; ?></span>

I think the problem is that the first page has the
PHP Syntax (Toggle Plain Text)
  1. if(isset($_POST['submit'])){
line which is causing the values not to be apssed to the next page? Does anyone know how I can keep the value $score available into the next page to display the score? I'm sooo stuck!! My 3rd day on this and I've read countless articles and tried different options and I'm still stuck. Please help!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
koldhands is offline Offline
4 posts
since Aug 2010
Sep 5th, 2010
0
Re: Help with PHP radio buttons
you can check isset['radioq1'] to check whether they have some value.. I think the result of addition you perform is always less than 5..
Last edited by hari.sarvothama; Sep 5th, 2010 at 2:58 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hari.sarvothama is offline Offline
15 posts
since Aug 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP include help
Next Thread in PHP Forum Timeline: Url Permalink





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC