PJ688 Posted October 27, 2011 Share Posted October 27, 2011 Hello all. I am new to PHP programming and am working on my first project. It's a very simple page that allows the user to send a message via text or email to other users. Unfortunately, I have hit a snag early on and have been unable to overcome it. I am trying to allow the user to select either text or email (via radio input). When this is done, the page is to present a text box with either a 185 or 500 character limit, depending on the selection. This is my code so far: <HTML> <HEAD> <TITLE>SMS System</TITLE> </HEAD> <BODY> <form method = "post"> How will you be sending your message?<br /> <input type = 'radio' name = 'method' value = 'text' checked = 'yes' /> Text to mobile <input type = 'radio' name = 'method' value = 'email' /> Email <br /> <br /> <br /> <?php $selected_radio = $_POST['method']; if($selected_radio == 'text'){ echo "Enter your message below:"; echo "<input type = 'text' id = 'message' name = 'message' maxlength = '185' />"; } ?> </form> </BODY> </HTML> I get an error on the line " $selected_radio = $_POST['method'];" that says "Undefined index: method". However, I clearly defined method as the radio selection. I have tried multiple ways to fix this issue: switching between double quotes and single quotes, using GET instead of POST, separating the code into a PHP file and an HTML file, and changing the name of "method" to something else. None of this has worked. I have read up on several websites, and they all accomplish what I am trying to do using the same technique I am using. If anyone here could be of assistance, I would be most appreciative. Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/ Share on other sites More sharing options...
The Little Guy Posted October 27, 2011 Share Posted October 27, 2011 Change this: $selected_radio = $_POST['method']; To this: $selected_radio = isset($_POST['method'])?$_POST['method']:""; The reason you have that error is because you have php strict mode on, and since you have not posted the form yet, "method" doesn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282729 Share on other sites More sharing options...
AyKay47 Posted October 27, 2011 Share Posted October 27, 2011 you need to check for the POSt index to be set before you try to set it to a variable, as stated above.. also, I notice that you do not have a submit button in your form.. I am assuming that you want your code to recognize what radio button is enabled as soon as the user clicks it? This cannot be done in PHP.. you will want to look into using AJAX or jquery AJAX API if this is the case Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282732 Share on other sites More sharing options...
PJ688 Posted October 27, 2011 Author Share Posted October 27, 2011 Thank you! The error is gone now. Now my if statement doesn't do anything, but I'll keep working on it. Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282738 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 27, 2011 Share Posted October 27, 2011 do you get the error before submitting the data or after? i dont see any submit buttons. try this. <html> <head> <title>SMS System</title> </head> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <?php if(isset($_POST['method']) && !empty($_POST['method'])) { $selected_radio = $_POST['method']; } else { echo " How will you be sending your message?<br /> <input type = 'radio' name = 'method' value = 'text' checked = 'yes' /> Text to mobile <input type = 'radio' name = 'method' value = 'email' /> Email <br /> <input type='submit' value='submit'/> <br /> <br /> <br />"; } if($selected_radio == 'text'){ echo "Enter your message below:<br />"; echo "<input type = 'text' id = 'message' name = 'message' maxlength = '185' /><br/> <input type='submit' value='submit'/> <br /> "; } ?> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282741 Share on other sites More sharing options...
PJ688 Posted October 27, 2011 Author Share Posted October 27, 2011 Yeah, I haven't implemented a Submit button yet. As someone above said, I am trying to make something happen when the user selects one of the radios, without having to Submit. If this is impossible, then I will be a sad panda. Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282742 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 27, 2011 Share Posted October 27, 2011 its possible using javascript. Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282744 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 27, 2011 Share Posted October 27, 2011 this is how it works using javascript <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> How will you be sending your message?<br /> <input type = 'radio' name = 'method' id='text' value = 'text' onchange="check()"/> Text to mobile <input type = 'radio' name = 'method' id='email' onchange="check()" value = 'email' /> Email <br /> <div style='display:none' id='msg'> Enter your message below:<br /> <input type = 'text' id = 'message' name = 'message' maxlength = '185' /><br/> <input type='submit' value='submit'/> <br /> </div> </form> <script> var text = document.getElementById("text"); var email = document.getElementById("email"); function check() { if(text.checked){ document.getElementById("msg").style.display = "block"; } else { document.getElementById("msg").style.display = "none"; } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282749 Share on other sites More sharing options...
AyKay47 Posted October 27, 2011 Share Posted October 27, 2011 read my post above and look into the methods that I mentioned.. you will want to use AJAX for this.. Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282751 Share on other sites More sharing options...
ZulfadlyAshBurn Posted October 27, 2011 Share Posted October 27, 2011 read my post above and look into the methods that I mentioned.. you will want to use AJAX for this.. OP trust this guy. look into jQuery API if you wish its pretty simple to implement. what i posted is pretty basic. Quote Link to comment https://forums.phpfreaks.com/topic/249925-new-php-programmer-needs-help/#findComment-1282756 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.