Click here to Skip to main content
15,916,835 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.

<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 class="form-group">
                    <label for="message">Your Message:</label>
                    <textarea class="form-control" name="message" placeholder="Enter your message here"></textarea>
                <input type="submit" name="submit" class="btn btn-success btn-lg" value="Send"/>
                <input type="hidden" name="submitted" value="true">

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
Updated 11-Mar-20 21:22pm
[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
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)

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