Jump to content

Error message - undefined index.....when running code


hance2105
Go to solution Solved by Psycho,

Recommended Posts

hello guys, 

 

i am having below error message for code...

 

 Notice: Undefined index: func in C:\wamp\www\buysmart_site\func.php on line 21 Call Stack # Time Memory Function Location 1 0.0007 148568 {main}( ) ..\add_prod_test.php:0 2 1.0181 162064 include( 'C:\wamp\www\buysmart_site\func.php' ) ..\add_prod_test.php:4

 

<?php//**************************************//     Page load dropdown results     ////**************************************function getprod_name(){$result = mysql_query("SELECT DISTINCT prod_name FROM tbl_prodstd") or die(mysql_error());  while($tier = mysql_fetch_array( $result ))   {  echo '<option value="'.$tier['prod_name'].'">'.$tier['prod_name'].'</option>';} } //**************************************//     First selection results     ////**************************************if($_GET['func'] == "prod_name" && isset($_GET['func'])) {    prod_name($_GET['drop_var']); } function prod_name($drop_var){      include_once('db_connect.php');$result = mysql_query("SELECT * FROM tbl_prodstd WHERE prod_name='$drop_var'") or die(mysql_error()); echo '<select name="prod_brand" id="prod_brand">     <option value=" " disabled="disabled" selected="selected">Choose one</option>';   while($drop_2 = mysql_fetch_array( $result )) { echo '<option value="'.$drop_2['prod_brand'].'">'.$drop_2['prod_brand'].'</option>';} echo '</select> ';    //echo '<input type="submit" name="submit" value="Submit" />';}?>

Link to comment
Share on other sites

  • Solution

And what is your question? Do you not understand what the error is? The error is exactly as it states - you are trying to reference the index of an array and that index does not exist. Here is the line in question

if($_GET['func'] == "prod_name" && isset($_GET['func'])) { 

So, the problem here is that unless you will ALWAYS be passing the variable 'func' on the query string every time this page is loaded you will get this error because the FIRST check you do in the if() condition is to test the value of $_GET['func']. So, if that index in the $_GET array is not set it produced the error. But, here's the thing, your SECOND validation checks to see if that variable is set. When you have an AND operator between two conditions the first condition is tested first - if it fails the PHP parser does not attempt to test the second condition (it's smart like that :) ). So you can fix your problem by simply switching the order of those conditions.

if(isset($_GET['func']) && $_GET['func'] == "prod_name") { 

It will then first test to see if the variable isset() - which would not produce the undefined index error since the whole point of isset() is to check if the variable is set or not. If it isn't set the second check to test the value will not be executed.

Edited by Psycho
Link to comment
Share on other sites

hey thanx...this solved the issue....but my second drop down is not being displayed now i dnt know why

 

i hv no error messages

 

here are my 2 pages

 

add_prod_test.php

<?php
session_start();
include('db_connect.php');
include('func.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add product</title>

<link href="CSS/add_prod.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#wait_1').hide();
$('#prod_name').change(function(){
 $('#wait_1').show();
 $('#result_1').hide();
      $.get("func.php", {
func: "prod_name",
drop_var: $('#prod_name').val()
      }, function(response){
        $('#result_1').fadeOut();
        setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
      });
     return false;
});
});

function finishAjax(id, response) {
  $('#wait_1').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
</script>

<script type="text/javascript">
$(document).ready(function()
{
$(".cat").change(function()
{
var id=$(this).val();
var dataString = 'id='+ id;

$.ajax
({
type: "POST",
url: "ajax.php",
data: dataString,
cache: false,
success: function(html)
{
$(".subcat").html(html);
} 
});

});

});
</script>

<link href="SpryAssets/SpryValidationTextarea.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="stylized" class="myform">

<form id="form" name="add_prod" method="post" action="addprod.php" enctype="multipart/form-data">

<h2 align="center"><b>- Add Product -</b></h2>

<h3 align="right"><a href="retailer_home.php"><img src="Images/Main Menu/home_icon.png" width="50" height="50" /></a></h3>

<table width="1000" border="0">
    <tr>
    <td><p align="right">Product Name</p></td>
    <td>
      <span id="spryselect3">
      <select name="prod_name" id="prod_name" class="prod_name">
      getprod_name();
 ?>
      </select>
      <span class="selectRequiredMsg">Please select an item.</span></span></td>
  </tr>
  <tr>
    <td><p align="right">Product Brand</p></td>
    <td>
      <input type="text" name="prod_brand" id="prod_brand" />
    </td>
  </tr>
  <tr>
    <td><p align="right">Product Description</p></td>
    <td><span id="sprytextarea1">
    <textarea name="prod_desc" id="prod_desc" cols="45" rows="5"></textarea>
    <span class="textareaRequiredMsg">A value is required.</span><span class="textareaMaxCharsMsg">Exceeded maximum number of characters.</span></span></td>
  </tr>
  <tr>
    <td><p align="right">Product Price (MRU)</p></td>
    <td><span id="sprytextfield4">
    <input type="text" name="prod_price" id="prod_price" />
    <span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span></td>
  </tr>
  <tr>
    <td><p align="right">Product Category</p></td>
    <td><span id="spryselect1">
      <select name="cat" class="cat">
        <option selected="selected">--Select Category--</option>
        <?php
include('db_connect.php');
$sql=mysql_query("select id,cat_name from tblprod_cat ORDER BY id ASC");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['cat_name'];
echo '<option value="'.$id.'">'.$data.'</option>';
} 
?>
      </select>
      <span class="selectRequiredMsg">Please select an item.</span></span></tr>
  <tr>
    <td><p align="right">Product Subcategory</p></td>
    <td><span id="spryselect2">
      <select name="subcat" class="subcat">
        <option>--Select Subcategory--</option>
      </select>
      <span class="selectRequiredMsg">Please select an item.</span></span></td>
  </tr>
  <tr>
    <td><p align="right">Product Weight/Capacity</p></td>
    <td><span id="sprytextfield5">
      <input type="text" name="prod_w_c" id="prod_w_c" />
      <span class="textfieldRequiredMsg">A value is required.</span></span></td>
  </tr>
  <tr>
    <td><p align="right">Please choose a file:</p></td>
    <td><input name="uploaded" type="file" /></td>
  </tr>
</table>
<p align="center">
  <input type="submit" class="button" name="button" id="button" value="<-- Add product -->"/>
</p>

</form>

</div>
<script type="text/javascript">
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "none", {validateOn:["blur"]});
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "none", {validateOn:["blur"]});
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4", "currency", {validateOn:["blur"]});
var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5", "none", {validateOn:["blur"]});
var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1", {validateOn:["blur"], minChars:0, maxChars:500});
var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1", {validateOn:["blur"]});
var spryselect2 = new Spry.Widget.ValidationSelect("spryselect2", {validateOn:["blur"]});
var spryselect3 = new Spry.Widget.ValidationSelect("spryselect3", {validateOn:["blur"]});
</script>
</body>
</html>

func.php

 

<?php//**************************************//     Page load dropdown results     ////**************************************function getprod_name(){$result = mysql_query("SELECT DISTINCT prod_name FROM tbl_prodstd") or die(mysql_error());  while($tier = mysql_fetch_array( $result ))   {  echo '<option value="'.$tier['prod_name'].'">'.$tier['prod_name'].'</option>';} } //**************************************//     First selection results     ////**************************************if(isset($_GET['func']) && $_GET['func'] == "prod_name") {     prod_name($_GET['drop_var']); } function prod_name($drop_var){      include_once('db_connect.php');$result = mysql_query("SELECT * FROM tbl_prodstd WHERE prod_name='$drop_var'") or die(mysql_error()); echo '<select name="prod_brand" id="prod_brand">     <option value=" " disabled="disabled" selected="selected">Choose one</option>';   while($drop_2 = mysql_fetch_array( $result )) { echo '<option value="'.$drop_2['prod_brand'].'">'.$drop_2['prod_brand'].'</option>';} echo '</select> ';    //echo '<input type="submit" name="submit" value="Submit" />';}?>

Link to comment
Share on other sites

Am I supposed to just read through your files and try to figure out what your second select field is and why it is not being displayed?

 

Look at where you are calling these functions. You aren't calling them within PHP

 

<table width="1000" border="0">
    <tr>
    <td><p align="right">Product Name</p></td>
    <td>
      <span id="spryselect3">
      <select name="prod_name" id="prod_name" class="prod_name">
      getprod_name();
 ?>

 

There's a closing PHP tag - but no opening tag. So, the function is treated as HTML

 

In another instance you put PHP code in, but there is no error handling. So it could be the query is returning 0 results.

 

      <select name="cat" class="cat">
        <option selected="selected">--Select Category--</option>
        <?php
include('db_connect.php');
$sql=mysql_query("select id,cat_name from tblprod_cat ORDER BY id ASC");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$data=$row['cat_name'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
?>

 

You really should revise how you are building your code. Here are my suggestions:

 

1. Create your functions so the RETURN the output to be displayed instead of echoing it. That is a bad practice.

 

2. Put all your PHP "logic" at the top of the page (or even in a separate PHP page) and just create variables with the output to be displayed.

 

Here's an example. Change the function getprod_name() to something like this:

 

function getprod_name()
{
    $query = "SELECT DISTINCT prod_name FROM tbl_prodstd";
    $result = mysql_query($query) or die(mysql_error());
    $output = '';
    while($row = mysql_fetch_array( $result ))
    {
        $output .= "<option value='{$row['prod_name']}'>{$row['prod_name']}</option>\n";
    }
    return $output;
}

Note: I use double quotes to define the string so I can put the variables in the string and put a line break (\n) at the end of the string. The latter makes your HTML code readable to assist in debugging.

 

Next. call the function at the top of the page (before any HTML output) where you need that select list and assign the output of that function to a variable.

 

$productOptions = getprod_name();

 

Lastly, just output that variable within the HTML output of the page

 

     <select name="prod_name" id="prod_name" class="prod_name">
     <?php echo $productOptions; ?>
     </select>

 

Following some sort of patter such as this makes your code cleaner and more manageable.

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.