-2

I'm making travel package website and I want to retrieve data using either GET, POST , REQUEST in php. but I get an error message saying "Array ( ) no Notice: Undefined index:"


Javascript function:

calculate total by simply multiplying the values from "package" and "# of person"


below is my code

<script type="text/javascript">

function totalPrice(){

document.getElementById("total").value= "CAD "+ 
(document.getElementById("package").value * document.getElementById("person").value);}   
</script>

<!-- package selection -->

<form action="test.php" method="get">


<select id="package" onchange="totalPrice();" >
      <option value="2300">Wild West (Banff, Jasper)</option>
      <option value="3300">East Coast(St.Johns)</option>
      <option value="1300">Winery Tour(Kelowna, Penticton)</option>
      <option value="2600">Northern Light(Yellowknife)</option>
 </select>

 <select id="person" onchange="totalPrice();">
          <option value="1">1 person</option>
          <option value="2">2 persons</option>
          <option value="3">3 persons</option>
          <option value="4">4 persons</option>
          <option value="5">5 persons</option>
          <option value="6">6 persons</option>
          <option value="7">7 persons</option>
          <option value="8">8 persons</option>

  </select>
  <input type="submit">

  </form>
  <!-- total Amount -->
  <label class="form" >Total Amount</label>
  <input size =40 type="text" name="total" id="total" disabled>

"test.php"

<h1>payment total</h1>

<?php

print_r($_GET);
if(!isset($_GET['total'])){
  echo "no";
}
echo $_GET['total'];
 ?>
M.K. Kim
  • 481
  • 1
  • 4
  • 17
  • There's no input with the name `total` in your form. (There's one outside your form.) – David Mar 17 '19 at 03:07
  • You have to submit the data to PHP, either by submitting your form or doing an AJAX request with Javascript. – Cfreak Mar 17 '19 at 03:09
  • Whilst I can understand why members want to close this question, I do not understand how it can be considered 'off topic' ? – ProEvilz Mar 17 '19 at 03:15

2 Answers2

1

In order for the field "total" to be included in what is sent to the test.php server you need to move the "total" input field inside of the form tag. Anything outside of the form tag will not be included.

Edit adding example:

<script type="text/javascript">

function totalPrice(){

document.getElementById("total").value= "CAD "+ 
(document.getElementById("package").value * document.getElementById("person").value);}   
</script>

<!-- package selection -->

<form action="test.php" method="get">


<select id="package" onchange="totalPrice();" >
      <option value="2300">Wild West (Banff, Jasper)</option>
      <option value="3300">East Coast(St.Johns)</option>
      <option value="1300">Winery Tour(Kelowna, Penticton)</option>
      <option value="2600">Northern Light(Yellowknife)</option>
 </select>

 <select id="person" onchange="totalPrice();">
          <option value="1">1 person</option>
          <option value="2">2 persons</option>
          <option value="3">3 persons</option>
          <option value="4">4 persons</option>
          <option value="5">5 persons</option>
          <option value="6">6 persons</option>
          <option value="7">7 persons</option>
          <option value="8">8 persons</option>

  </select>
  <input type="submit">

  <!-- total Amount -->
  <label class="form" >Total Amount</label>
  <input size =40 type="text" name="total" id="total" disabled>

  </form>
James Anderbard
  • 374
  • 2
  • 6
  • 20
0

You need to make sure that any data you want to be transmitted on submission is within the <form> </form> tags. If you leave it out, the data won't be passed over...

For example, lets imagine you had two form tags (A and B), both with a submit button. If you left out <input size =40 type="text" name="total" id="total" disabled> from both forms, when you pressed submit on either of them, which form would the total be sent within?

Definition of a <form> tag:

The HTML element represents a document section that contains interactive controls for submitting information to a web server. (contains being the key word here)

I have moved the input into the correct place below:

function totalPrice(){

document.getElementById("total").value= "CAD "+ 
(document.getElementById("package").value * document.getElementById("person").value);}   
</script>

<!-- package selection -->

<form action="test.php" method="get">


<select id="package" onchange="totalPrice();" >
      <option value="2300">Wild West (Banff, Jasper)</option>
      <option value="3300">East Coast(St.Johns)</option>
      <option value="1300">Winery Tour(Kelowna, Penticton)</option>
      <option value="2600">Northern Light(Yellowknife)</option>
 </select>

 <select id="person" onchange="totalPrice();">
          <option value="1">1 person</option>
          <option value="2">2 persons</option>
          <option value="3">3 persons</option>
          <option value="4">4 persons</option>
          <option value="5">5 persons</option>
          <option value="6">6 persons</option>
          <option value="7">7 persons</option>
          <option value="8">8 persons</option>

  </select>

  <!-- total Amount -->
  <label class="form" >Total Amount</label>
  <input size =40 type="text" name="total" id="total" disabled>

  <input type="submit">

  </form> <-- END OF FORM -->

Also, here is a better way to handle things on the PHP side because whilst you may be checking if the total is set, if it is not, it will still error out in the way you have it.

<?php

print_r($_GET);

if(isset($_GET['total'])){
  echo $_GET['total'];
} else {
  echo 'No total';
}
?>

EDIT

You also need to make the input not disabled as disabled inputs wont be submitted. If you want to make the user unable to edit the input the change:

<input size =40 type="text" name="total" id="total" disabled>

to

<input size =40 type="hidden" name="total" id="total">

This will hide it from the page too. If you still want it visible, you can set it like this:

<input size =40 type="text" name="total" id="total" readonly="readonly">
ProEvilz
  • 5,310
  • 9
  • 44
  • 74