Hi, im newbie here.
Please anyone can help me solved this problem?
Notice: Undefined variable: jumlah_mainboard in C:\xampp\htdocs\latihan\pesan.php on line 31

Notice: Undefined index: harga_mainboard in C:\xampp\htdocs\latihan\pesan.php on line 12

Notice: Undefined index: harga_processor in C:\xampp\htdocs\latihan\pesan.php on line 14

Notice: Undefined index: harga_ram in C:\xampp\htdocs\latihan\pesan.php on line 16

Notice: Undefined index: harga_hardisk in C:\xampp\htdocs\latihan\pesan.php on line 18

Notice: Undefined index: jumlah_mainboard in C:\xampp\htdocs\latihan\pesan.php on line 22

Notice: Undefined index: jumlah_processor in C:\xampp\htdocs\latihan\pesan.php on line 24

Notice: Undefined index: jumlah_ram in C:\xampp\htdocs\latihan\pesan.php on line 26

Notice: Undefined index: jumlah_hardisk in C:\xampp\htdocs\latihan\pesan.php on line 28

Notice: Undefined index: diskon in C:\xampp\htdocs\latihan\pesan.php on line 40

<?php
	/*inisiasi variabel yang digunakan
	nama peralatan
	*/
	$mainboard = "Mainboard";
	$processor = "Processor";
	$ram = "Ram";
	$hardisk = "Hardisk";

//harga per unit peralatan

$harga_mainboard = $_POST['harga_mainboard'];

$harga_processor = $_POST['harga_processor'];

$harga_ram = $_POST['harga_ram'];

$harga_hardisk = $_POST['harga_hardisk'];

//Jumlah peralatan yang ada

$jumlah_mainboard = $_POST['jumlah_mainboard'];

$jumlah_processor = $_POST['jumlah_processor'];

$jumlah_ram = $_POST['jumlah_ram'];

$jumlah_hardisk = $_POST['jumlah_hardisk'];

//Total Harga per jenis peralatan
$total_mainboard = $harga_mainboard * $jumlah_mainboard;
$total_processor = $harga_processor * $jumlah_processor;
$total_ram = $harga_ram * $jumlah_ram;
$total_hardisk = $harga_hardisk * $jumlah_hardisk;

//hitung peralatan total
$total_harga = $total_mainboard + $total_processor + $total_ram + $total_hardisk;

//besar discount untuk peralatan
$diskon = $_POST['diskon'];

//jumlah total diskon yang diberikan
$nilai_diskon = ($diskon * $total_harga)/100;

//jumlah yang harus di bayar

$total_harga_bayar = $total_harga - $nilai_diskon;
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>daftar Peralatan yang dibeli</title>
</head>
<body>
<center>
<form action="pesan.php" method="post">
<table border="1" cellspacing="0" cellpadding="3">
<tr>
	<td colspan="4" align="center" valign="middle">
	<b>Daftar Pemesanan Hardware Komputer </b></td>
</tr>
<tr align="center">
	<td width="136"><b>Nama Hardware</b></td>
	<td width="49"><b>Jumlah</b></td>
	<td width="109"><b>Harga Satuan</b></td>
	<td Width="96"><b>Jumlah Harga</b></td>
</tr>
<tr>
	<td align="left"><?php echo $mainboard; ?></td>
	<td align="right"><input name="jumlah_mainboard"
	type="text" size="3" value="<?php echo 	$jumlah_mainboard; ?>" /></td>
	<td align="right"><input name="harga_mainboard"
type="text" size="15" value="<?php echo $harga_mainboard; ?>" /></td>
	<td align="right"><input name="total_mainboard"
type="text" size="3" value="<?php echo $total_mainboard; 	?>" /></td>
</tr>
<tr>
	<td align="left"><?php echo $processor; ?></td>
	<td align="right"><input name="jumlah_processor"
	type="text" size="3" value="<?php echo 	$jumlah_processor; ?>" /></td>
	<td align="right"><input name="harga_processor"
type="text" size="15" value="<?php echo $harga_processor; ?>" /></td>
	<td align="right"><input name="total_processor"
type="text" size="3" value="<?php echo $total_processor; 	?>" /></td>
</tr>
<tr>
	<td align="left"><?php echo $ram; ?></td>
	<td align="right"><input name="jumlah_ram"
	type="text" size="3" value="<?php echo 	$jumlah_ram; ?>" /></td>
	<td align="right"><input name="harga_ram"
type="text" size="15" value="<?php echo $harga_ram; ?>" /></td>
	<td align="right"><input name="total_ram"
type="text" size="3" value="<?php echo $total_ram; 	?>" /></td>
</tr>
<tr>
	<td align="left"><?php echo $hardisk; ?></td>
	<td align="right"><input name="jumlah_hardisk"
	type="text" size="3" value="<?php echo 	$jumlah_hardisk; ?>" /></td>
	<td align="right"><input name="harga_hardisk"
type="text" size="15" value="<?php echo $harga_hardisk; ?>" /></td>
	<td align="right"><input name="total_hardisk"
type="text" size="3" value="<?php echo $total_hardisk; 	?>" /></td>
</tr>
<tr>
	<td colspan="3" align="right">Total Harga</td>
	<td align="right"><?php echo $total_harga; ?></td>
</tr>
<tr>
	<td colspan="3" align="right">
	  <p>Diskon
  <input name="Diskon" type="text" size="2" value="<?php echo $diskon;?>" />
	    %</p></td>
	<td align="right"><?php echo $nilai_diskon; ?></td>
</tr>
<tr>
	<td colspan="3" align="right">Jumlah harus dibayar</td>
	<td align="right"><?php echo $total_harga_bayar;?></td>
</tr>
<tr>
<td colspan="4" align="center">
<input type="submit" value="OK" />
<input type="reset" value="Batal" /></td>
</tr>
</table>
</form>
</center>
</body>

    This "Undefined variable" means that you are trying to use a variable before you have created it.

    This will also throw the same error:

    <?php
    
    echo "Hello, my name is {$name}.";

    To resolve it, just define the variable before its first use:

    <?php
    
    $name = 'Jon Doe';
    echo "Hello, my name is {$name}.";

    The "Undefined index" error means that you're trying to reference an element in an array (or an object with array accessibility) which doesn't exist.

    This will have the same error:

    <?php
    
    $array = array('first_name' => 'Jon', 'last_name' => 'Doe', 'age' => 35);
    
    echo "Hello, my full name is {$array['first_name']} {$array['middle_initial']} {$array['last_name']} and I'm {$array['age']} years old.";

    If you do not need this error to show, you can either initialize the array with empty strings (or null) to prevent the error, or use the hush operator (the "@" symbol) to suppress the error:

    <?php
    
    // Initialize the array:
    $array = array('first_name' => 'Jon', 'middle_initial' => '', 'last_name' => 'Doe', 'age' => 35);
    // OR
    // Use hush operator:
    echo "Hello, my name is " . @$array['first_name'] . " " . @$array['middle_initial'] . " " . @$array['last_name'] . " and I'm " . @$array['age'] . " years old.";

    Otherwise you will want to inspect the array with either [man]print_r[/man] or [man]var_dump[/man] to see the elements in the array and change your code accordingly.

      abi_ug;11040567 wrote:

      Hi, im newbie here.
      Please anyone can help me solved this problem?

      <?php
      	/*inisiasi variabel yang digunakan
      	nama peralatan
      	*/
      	$mainboard = "Mainboard";
      	$processor = "Processor";
      	$ram = "Ram";
      	$hardisk = "Hardisk";
      	[COLOR="#FF0000"]$_POST = "0";[/COLOR]
      	//harga per unit peralatan
      
      $harga_mainboard = $_POST['harga_mainboard'];
      
      $harga_processor = $_POST['harga_processor'];
      
      $harga_ram = $_POST['harga_ram'];
      
      $harga_hardisk = $_POST['harga_hardisk'];
      
      //Jumlah peralatan yang ada
      
      $jumlah_mainboard = $_POST['jumlah_mainboard'];
      
      $jumlah_processor = $_POST['jumlah_processor'];
      
      $jumlah_ram = $_POST['jumlah_ram'];
      
      $jumlah_hardisk = $_POST['jumlah_hardisk'];
      
      //Total Harga per jenis peralatan
      $total_mainboard = $harga_mainboard * $jumlah_mainboard;
      $total_processor = $harga_processor * $jumlah_processor;
      $total_ram = $harga_ram * $jumlah_ram;
      $total_hardisk = $harga_hardisk * $jumlah_hardisk;
      
      //hitung peralatan total
      $total_harga = $total_mainboard + $total_processor + $total_ram + $total_hardisk;
      
      //besar discount untuk peralatan
      $diskon = $_POST['diskon'];
      
      //jumlah total diskon yang diberikan
      $nilai_diskon = ($diskon * $total_harga)/100;
      
      //jumlah yang harus di bayar
      
      $total_harga_bayar = $total_harga - $nilai_diskon;
      ?>
      
      
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>daftar Peralatan yang dibeli</title>
      </head>
      <body>
      <center>
      <form action="pesan.php" method="post">
      <table border="1" cellspacing="0" cellpadding="3">
      <tr>
      	<td colspan="4" align="center" valign="middle">
      	<b>Daftar Pemesanan Hardware Komputer </b></td>
      </tr>
      <tr align="center">
      	<td width="136"><b>Nama Hardware</b></td>
      	<td width="49"><b>Jumlah</b></td>
      	<td width="109"><b>Harga Satuan</b></td>
      	<td Width="96"><b>Jumlah Harga</b></td>
      </tr>
      <tr>
      	<td align="left"><?php echo $mainboard; ?></td>
      	<td align="right"><input name="jumlah_mainboard"
      	type="text" size="3" value="<?php echo 	$jumlah_mainboard; ?>" /></td>
      	<td align="right"><input name="harga_mainboard"
      type="text" size="15" value="<?php echo $harga_mainboard; ?>" /></td>
      	<td align="right"><input name="total_mainboard"
      type="text" size="3" value="<?php echo $total_mainboard; 	?>" /></td>
      </tr>
      <tr>
      	<td align="left"><?php echo $processor; ?></td>
      	<td align="right"><input name="jumlah_processor"
      	type="text" size="3" value="<?php echo 	$jumlah_processor; ?>" /></td>
      	<td align="right"><input name="harga_processor"
      type="text" size="15" value="<?php echo $harga_processor; ?>" /></td>
      	<td align="right"><input name="total_processor"
      type="text" size="3" value="<?php echo $total_processor; 	?>" /></td>
      </tr>
      <tr>
      	<td align="left"><?php echo $ram; ?></td>
      	<td align="right"><input name="jumlah_ram"
      	type="text" size="3" value="<?php echo 	$jumlah_ram; ?>" /></td>
      	<td align="right"><input name="harga_ram"
      type="text" size="15" value="<?php echo $harga_ram; ?>" /></td>
      	<td align="right"><input name="total_ram"
      type="text" size="3" value="<?php echo $total_ram; 	?>" /></td>
      </tr>
      <tr>
      	<td align="left"><?php echo $hardisk; ?></td>
      	<td align="right"><input name="jumlah_hardisk"
      	type="text" size="3" value="<?php echo 	$jumlah_hardisk; ?>" /></td>
      	<td align="right"><input name="harga_hardisk"
      type="text" size="15" value="<?php echo $harga_hardisk; ?>" /></td>
      	<td align="right"><input name="total_hardisk"
      type="text" size="3" value="<?php echo $total_hardisk; 	?>" /></td>
      </tr>
      <tr>
      	<td colspan="3" align="right">Total Harga</td>
      	<td align="right"><?php echo $total_harga; ?></td>
      </tr>
      <tr>
      	<td colspan="3" align="right">
      	  <p>Diskon
        <input name="Diskon" type="text" size="2" value="<?php echo $diskon;?>" />
      	    %</p></td>
      	<td align="right"><?php echo $nilai_diskon; ?></td>
      </tr>
      <tr>
      	<td colspan="3" align="right">Jumlah harus dibayar</td>
      	<td align="right"><?php echo $total_harga_bayar;?></td>
      </tr>
      <tr>
      <td colspan="4" align="center">
      <input type="submit" value="OK" />
      <input type="reset" value="Batal" /></td>
      </tr>
      </table>
      </form>
      </center>
      </body>

      thx i have try to create the value of $POST
      I create $
      POST value = "0"
      but why it doesnt count the result?

        The change you made above will destroy the entire $_POST array, meaning the rest of your script won't be able to use it to extract the user's POST'ed form data.

        Instead, you should (in my opinion, at least) check each piece of external data before you attempt to use it. I like to accomplish this by writing code like the following (with comments added to clarify the intent):

        // $foo is expected to be a string
        $foo = (isset($_POST['foo']) ? $_POST['foo'] : '');
        
        // $bar is expected to be an integer
        $bar = (isset($_POST['bar']) ? (int)$_POST['bar'] : 0);

          While all the above advice is good, you still need to find out why you are not getting the expected form fields when you submit the form -- assuming that's what's going on here? You might want to stick the following at the beginning of the php form-handler just to see what is coming in (or if nothing is), and then debug based on that info:

          <?php
          echo "<pre>DEBUG of POST:\n".print_r($_POST, true)."</pre>"; // delete when done debugging
          

          If your form fields are not showing up in that output at all, then we need to figure out why. If they are there but with different names (remember they are case-sensitive), then you need to fix those key names.

            NogDog;11040587 wrote:

            While all the above advice is good, you still need to find out why you are not getting the expected form fields when you submit the form -- assuming that's what's going on here? You might want to stick the following at the beginning of the php form-handler just to see what is coming in (or if nothing is), and then debug based on that info:

            <?php
            echo "<pre>DEBUG of POST:\n".print_r($_POST, true)."</pre>"; // delete when done debugging
            

            If your form fields are not showing up in that output at all, then we need to figure out why. If they are there but with different names (remember they are case-sensitive), then you need to fix those key names.

            thank u so much for the answer

            i have set :

            $_POST = NULL;

            but it same the counting result doesnt appear. i just can input the value, but it doesnt show the result.
            how i can finish this?

              abi_ug wrote:

              i have set :

              $POST = NULL;

              Again, that change will destroy the entire [font=monospace]$POST[/font] array, meaning the rest of your script won't be able to use it to extract the user's POST'ed form data.

                Write a Reply...