New PHP Programmer needs help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PJ688
Forum Newbie
Posts: 5
Joined: Thu Oct 27, 2011 11:50 am

New PHP Programmer needs help

Post 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!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New PHP Programmer needs help

Post 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' />";
    }
}
PJ688
Forum Newbie
Posts: 5
Joined: Thu Oct 27, 2011 11:50 am

Re: New PHP Programmer needs help

Post by PJ688 »

So how do I see which has been selected if the user has not technically "submitted" any information?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: New PHP Programmer needs help

Post by Celauran »

If you want to do it dynamically, you'll need to use Javascript.
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: New PHP Programmer needs help

Post 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..
uday8486
Forum Newbie
Posts: 22
Joined: Fri Oct 28, 2011 11:42 pm
Location: Pune, India

Re: New PHP Programmer needs help

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