Page 1 of 1

New PHP Programmer needs help

Posted: Thu Oct 27, 2011 11:56 am
by PJ688
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:

Code: Select all

<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!

Re: New PHP Programmer needs help

Posted: Thu Oct 27, 2011 11:59 am
by Celauran
You're not checking if the form has been submitted. Trying to assign $_POST['anything'] to a variable when $_POST is empty isn't going to work.

Code: Select all

if (!empty($_POST) && isset($_POST['method']))
{
    $selected_radio = $_POST['method'];
    if ($selected_radio == 'text')
    {
        echo "Enter your message below:";
        echo "<input type = 'text' id = 'message' name = 'message' maxlength = '185' />";
    }
}

Re: New PHP Programmer needs help

Posted: Thu Oct 27, 2011 12:03 pm
by PJ688
So how do I see which has been selected if the user has not technically "submitted" any information?

Re: New PHP Programmer needs help

Posted: Thu Oct 27, 2011 12:09 pm
by Celauran
If you want to do it dynamically, you'll need to use Javascript.

Re: New PHP Programmer needs help

Posted: Fri Oct 28, 2011 1:05 am
by Gopesh
Hi,U can show textbox based on the selection of the radio button dynamically using jquery.Check

Code: Select all

<HTML>
    <HEAD>
        <TITLE>SMS System</TITLE>
       
        <script type="text/javascript" src="jquery-1.6.4.min.js"></script>
        <script>
		$(document).ready(function(){
    $("#textmsg").css("display","none");
        $(".message1").click(function(){
        if ($('input[name=method]:checked').val() == "text" ) {
            $("#textmsg").slideDown("fast"); //Slide Down Effect
        } else {
            $("#textmsg").slideUp("fast");  //Slide Up Effect
        }
     });
});
		</script>
    </HEAD>
    
    <BODY>
        <form  action="test.php" method = "post">
        How will you be sending your message?<br />
        <input type="radio" name="method" value="text" class="message1" /> Yes
    <input type="radio" name="method" value="email" class="message1" /> No
        <div id="textmsg">
        Enter your message below:<br />
        <input type = 'text' id = 'message' name = 'message' maxlength = '185' />
        </div>
        <br />
        <br />
        <br />
        
        </form>
    </BODY>
</HTML>

Hope it helps u..

Re: New PHP Programmer needs help

Posted: Sat Oct 29, 2011 2:19 am
by uday8486
You want set the maxlenth depending on selection i guess.

you do this

Code: Select all

<script>
                $(document).ready(function(){
     
        $(".message1").click(function(){
        if ($('input[name=method]:checked').val() == "email" ) {
            $("#textmsg").attr("maxlength",'500');  
        } else {
            $("#textmsg").attr("maxlength",'185');  
        }
     });
});
                </script>
So you set the maxlength according to selection in radio button.

Hope solves your query!