Click here to Skip to main content
14,331,194 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello im trying to get some variables from a form to the php part of the code.
I am getting these errors:
Notice: Undefined index: number in C:\xampp\htdocs\sms\textmessage.php on line 10
Notice: Undefined index: message in C:\xampp\htdocs\sms\textmessage.php on line 11


That's my code. does anyone know why I get these errors? I can't seem to find why that happens as I think im doing everything correctly.

PS: Everything are in one file.

HTML CODE:
<pre><div class="container">
	<div class="row">
		<div class="col-md-6 col-md-offset-3 textForm">
			<h1>Message alert serivce</h1>
			 <?php echo $result; ?>
			<p class="lead">Fill all the required fields</p>

			<form method="POST">
                
				<div class="form-group">
                    <label for="number">Receiver's Number:</label><input type="number"  name="number" class="form-control" placeholder="Insert number here" /> 
				</div>				 
			
				<div class="form-group">
                    <label for="message">Your Message:</label>
                    <textarea class="form-control" name="message" placeholder="Enter your message here"></textarea>
				</div>
                
                <input type="submit" name="submit" class="btn btn-success btn-lg" value="Send"/>
                <input type="hidden" name="submitted" value="true">
				
			</form>			 
		</div>		 
	</div>	 
</div>

and that is my PHP code where the errors are:
//Get form data

$number1 = $_POST['number']; //Line 10
$message = $_POST['message']; //Line 11
$message = urlencode($message); //Test that the message is encoded correctly. Php will use urlencode the message


What I have tried:

i dont see the mistake so I don't know what to try
Posted
Updated 12-Feb-17 15:33pm
Comments
User 59241 12-Feb-17 19:44pm
   
Here is a clue: html - PHP Undefined Index - Stack Overflow[^]. Google still works.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Since those errors occurred at lines 10 and 11, they must have been placed before the html code, they are undefined on first load as there is no post yet. So you should always check for the existence of any post using
isset()
before accessing it, e.g.
if(isset($_POST['number']) && isset($_POST['message'])){
  $number1 = $_POST['number']; //Line 10
  $message = $_POST['message']; //Line 11
  $message = urlencode($message); 
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100