7 Replies - 485 Views - Last Post: 29 October 2011 - 11:24 AM Rate Topic: -----

#1 max21b  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-October 11

checkbox ?

Posted 22 October 2011 - 03:47 PM

This code is using PHP_SELF. At run-time, you can click summit and it will show an input error on same page
- - You did not select a credit card type.

This is the result when you select and than summit
- - Your credit card type is: Array

As you see the word Array is not the desired result. The same type code work for everything under the sun in my program but not for checkbox because I don't know how to insert the code properly. I been stuck here for many hours and nothing I could think of works. Could someone help me to correct this code.

Also, the box after the "Select a card:" text should not be there and all I tried did not remove it.

Thanks in advance for any help

<html><head>
<body>

<?php

$err=array();
 if(isset($_REQUEST['submit']))	{  checkUser_Input();	//  if submitted   if(count($err) != 0)		{   Begin_Page(); }	//  re-display
	else {						//  show results
		  $card_type=$_POST[cardType];
print "<b>Your credit card type is: $card_type<br><br>";

	}  }   else{Begin_Page();  }			//  show page

//  ......................................  checkUser_Input for checkbox
//  ......................................  checkUser_Input for checkbox

 function checkUser_Input()   {
 global $err;

     if($_POST["cardType"] == "") {
         $err['cardType']="You did not select a credit card type.";
     }
   }
   function Begin_Page(){
   global $err; ?>
<b>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
											<!-- Master/Visa  -->
<b>Select a card:</b>
<input type="checkbox" name="cardType[]" value="<?php echo  $_POST[cardType]; ?>">
							<?php echo $err['cardType']; ?><br />

<input type="checkbox" name="cardType[]" value="visa" />visa<br />
<input type="checkbox" name="cardType[]" value="mastercard" />mastercard
<br />
<br />

  <input type="reset">
  <input type="submit" name="submit">
<br />
<form>

<?php
}
?>

</b>
</div>
</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: checkbox ?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: checkbox ?

Posted 22 October 2011 - 04:00 PM

Wouldn't it make more sense to use radio buttons here? A credit card can't be both a Visa and Mastercard at the same time.

Also, the selected element(s) will be the elements in $card_type, which is an array.
Was This Post Helpful? 1
  • +
  • -

#3 max21b  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-October 11

Re: checkbox ?

Posted 23 October 2011 - 05:40 PM

Quote

Wouldn't it make more sense to use radio buttons here?

Yes, but this is only an example of something I want to try to do. My words were wrong but my code is strong Just kidding macosxnerd101 :)

Quote

A credit card can't be both a Visa and Mastercard at the same time.

Now you really got me to thinking ....

Quote

Also, the selected element(s) will be the elements in $card_type, which is an Array.

You hit the nail on the head macosxnerd101 ... **ARRAY** ... Now I remember reading at the PHP manual that in a form the data is automatically given to PHP as key and value... I think we both overlooked "foreach" and what it is used for.

	foreach($card_type as $key=>$value)
	{
	print "Your selected value is: $value";
 }



I'm talking hours of thinking about your post, than it finally kick-in. For he who did not know, look no farther. Here's the working code as of posting it right now. The PHP_SELF will VALIDATE checkbox in an very strong fashion. Enjoy!

Thank you Sir

<html><head>
<body>

<?php

$err=array();
 if(isset($_REQUEST['submit']))	{  checkUser_Input(); 
 if(count($err) != 0)		{   Begin_Page(); }
	else {
		  $card_type=$_POST[cardType];

	foreach($card_type as $key=>$value){  // <-- <-- <-- <-- <-- <-- 
	print "<b>Your selected value is:<font color=red>$value</font><br><br>";  }

	}  }   else{Begin_Page();  }

//  ......................................................  VALIDATE checkbox
//  ......................................................  VALIDATE checkbox

 function checkUser_Input()   {
 global $err;

     if($_POST["cardType"] == "") {
         $err['cardType']="Invalid: - Please make an selection.";
     }
   }
   function Begin_Page(){
   global $err; ?>
<b>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<b>Select a card:</b>
<input type="checkbox" name="cardType[]" value="<?php echo  $_POST[cardType]; ?>">
												

<?php echo $err['cardType']; ?><br />
<input type="checkbox" name="cardType[]" value="visa" />visa<br />
<input type="checkbox" name="cardType[]" value="mastercard" />mastercard
<br />
<br />
  <input type="reset">
  <input type="submit" name="submit">
<br />
<form>

<?php  } ?>

</b>
</body>
</html>

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: checkbox ?

Posted 23 October 2011 - 07:23 PM

A few things still. First, please properly indent your code. Not to be rude, but it is a nightmare to read and follow. Make it easy on yourself and anyone reading your code by properly formatting it.

Second, please separate your HTML from your PHP. They are two very different technologies, and separating helps you make them both more maintainable.

Third, avoid using $_SERVER here, as this opens up your site to cross-site scripting exploits. Fourth, avoid $_REQUEST because cookie data is mixed in with the $_GET and $_POST data, and the cookie data overrides the get and post data. Better to check if the $_GET or $_POST element is set.

Your code really has a lot you still need to clean up before releasing this into production.
Was This Post Helpful? 0
  • +
  • -

#5 max21b  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-October 11

Re: checkbox ?

Posted 24 October 2011 - 08:17 AM

Mixing of technologies is a issue? If I did not mix HTML with PHP this code would not work and I would have nothing to-date. About the rest, I have to wait until this coming next week-end to follow your suggestions. You can't PHP while loading trucks 12 hours per day. I bet even programming for a living is very time consuming.

Anyway, I'm sure mixing everything would be bad for organization, but mixing properly, only where needed or for testing your PHP at home ... this shouldn't be a major issue and if it is i need to see proof. I learn better by example. Could you post the code that correct my coding style, mixed and un-mixed?

It's count-down time
See you latter
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,464
  • Joined: 27-December 08

Re: checkbox ?

Posted 24 October 2011 - 08:23 AM

Quote

Anyway, I'm sure mixing everything would be bad for organization, but mixing properly, only where needed or for testing your PHP at home

Let's step back for a minute. Not to be rude, but I can hardly read your code because it is so poorly formatted. Mixing languages in the same file does not help with the readability of your code. Plus, mixing makes it hard to focus on either the design or the programming aspects. By separating, you can focus on a clean design. Then you make it easier to modify the HTML templates accordingly with PHP. CTphpnwb's tutorial provides good ways to separate. I'll leave you to check it out and make the corrections.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2895
  • View blog
  • Posts: 10,028
  • Joined: 08-August 08

Re: checkbox ?

Posted 24 October 2011 - 08:32 AM

View Postmax21b, on 24 October 2011 - 11:17 AM, said:

Mixing of technologies is a issue? If I did not mix HTML with PHP this code would not work and I would have nothing to-date.

Did you even click on the link? It is NEVER necessary to mix the two. Sure, we've all done it, but that's just because we're trying to take a shortcut. That shortcut usually leads to problems like yours where you have less than 50 lines of code and you can't read/understand it well enough to debug it.
Was This Post Helpful? 1
  • +
  • -

#8 max21b  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-October 11

Re: checkbox ?

Posted 29 October 2011 - 11:24 AM

I'm checking out the link today. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1