Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
HTML
<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:
PHP
//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 11-Mar-20 21:22pm
Comments
[no name] 12-Feb-17 19:44pm    
Here is a clue: html - PHP Undefined Index - Stack Overflow[^]. Google still works.

1 solution

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); 
}
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900