Jump to content

New PHP Programmer needs help


PJ688

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.  :-[

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.