Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Undefined index ERROR?

Featured Replies

I am receiving a Undefined index ERROR, but am unsure why. Any reasons why?

 

 

<?php
$msg = "";
include ("connect_to_mysql.php");

if ($_POST['title'] != "") {
$title = $_POST['title'];
$score = $_POST['score'];
$next_game = $_POST['next_game'];
$words = $_POST['words'];
$words = stripslashes($words);
$title2 = $_POST['title2'];
$words2 = $_POST['words2'];
$words2 = stripslashes($words2);

$query = mysql_query("INSERT INTO hometext (title, score, next_game, words, title2, words2) VALUES ('$title','$score','$next_game','$words','$title2','$words2')") or die (mysql_error());
$msg = "entered";
}


?>

<html>
<head>
<title>Add</title>
<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
        // General options
        mode : "textareas",
        theme : "advanced",
	 elements : "content",
	 theme_advanced_font_sizes : "Big text=30px,Small text=small,My Text Size=.mytextsize"

});
</script>

</head>

<body>

<div>

<p>Add post to database</p>
<form action="add.php" method="POST">
	Title:<br />
	<input type="text" name="title" /><br /><br />
	Score:<br />
	<input type="text" name="score" /><br /><br />
	Next Game:<br />
	<input type="text" name="next_game" /><br /><br />
	Article:<br />
	<textarea name="words" class="tinymce" cols="50" rows="10"></textarea>
	<br />
	Title 2:<br />
	<input type="text" name="title2" /><br /><br />
	Article 2:<br />
	<textarea name="words2" class="tinymce" cols="50" rows="10"></textarea>
	<br /><br />
	<input type="submit" name="submit" value="Submit" />
	<?php echo $msg; ?>
</form>

</div>



</body>

</html>

Write ur varibles like this to avoid index errors

 

$name = (isset($_POST['name'])) ? $_POST['name'] : false;

 

or you can write a simple function to handle input values, heres my function to do just that

 

function input($type, $value, $forDb = false) {

 if ($type && $value) {

   switch ($type) {

     case 'post' :

       if (isset ($_POST[$value])) {

         if ($forDb === true) {

           if (get_magic_quotes_gpc() == 1) {

             return @ mysqli_real_escape_string(connect(), stripslashes($_POST[$value]));

           }

           else {

             return @ mysqli_real_escape_string(connect(), $_POST[$value]);

           }

         }

         else {

           return $_POST[$value];

         }

       }

       break;

     case 'get' :

       if (isset ($_GET[$value])) {

         if ($forDb === true) {

           if (get_magic_quotes_gpc() == 1) {

             return @ mysqli_real_escape_string(connect(), stripslashes($_GET[$value]));

           }

           else {

             return @ mysqli_real_escape_string(connect(), $_GET[$value]);

           }

         }

         else {

           return $_GET[$value];

         }

       }

       break;

   }

 }

}

The first time you run this script, the form won't have been submitted so the $_POST indices won't be recognised. You will only be able to check if they are set with isset. One way round this would be to use a hidden field in your form which you can check for. Replace

if ($_POST['title'] != "") {

with

if (isset($_POST['submitted'])) {

and add

<input type="hidden" name="submitted" value="TRUE" />

to the end of your form.

The first time you run this script, the form won't have been submitted so the $_POST indices won't be recognised. You will only be able to check if they are set with isset. One way round this would be to use a hidden field in your form which you can check for. Replace

if ($_POST['title'] != "") {

with

if (isset($_POST['submitted'])) {

and add

<input type="hidden" name="submitted" value="TRUE" />

to the end of your form.

actually an extra hidden field is not needed all he has to do is check for the submit button being clicked like this

 

if (isset($_POST['submit'])) {
//Your code
}

He means you're repeating your code. DRY = 'dont repeat yourself'

 

There are more effecient ways of writing that.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.