0

I created a 3-step form. the form is aimed to determine the price according to the number of employees entered. the user is entering the number of employees in the first step. JavaScript calculates the price according to this value. The index1.php form looks like this:

<form id="empnum" class="form-horizontal" method="GET" action="index2.php">
  <label class="control-label">employee number</label>
   <div class="controls">
  <input type="text" class="span6 " id="employee_number" name="employee_number" required>
     </div>
 <button type="submit" class="btn btn-success">Calculate</button>
  </form>

The js code is shown below;

function calculate(){
$.ajaxSettings.cache = false;
var employee_number = $("#employee_number").val();

function time(s) {

    if(s>60){
        hour=Math.floor(s/60);
        mnts=s-(hour*60);
        if(mnts==0){result= hour +' hour';}else{result= hour +' hour '+ mnts + ' mnts' ;}
    }
    else{result= s +' mnts';}
    return result; 
}

if (employee_number.length > 0)
{
    // -------------------
    a1=(employee_number*5)
    a2=(employee_number*10)
    a3=(employee_number*15)
    $(".a1").html(time(a1)+" /month");
    $(".a2").html(time(a2)+" /month");
    $(".a3").html(time(a3)+" /month");

    // -------------------

    a1=(employee_number*10)
    a2=(employee_number*20)
    a3=(employee_number*40)
    $(".b1").html(time(a1)+" /month");
    $(".b2").html(time(a2)+" /month");
    $(".b3").html(time(a3)+" /month");


    // -------------------

    if(employee_number>9 && employee_number<50){

        $(".c3").html(time(employee_number*10)+" /month");
    }
    else if(employee_number>49 && employee_number<250){

        $(".c3").html(time(employee_number*15)+" /month");
    }
    else if(employee_number>249){

        $(".c3").html(time(employee_number*20)+" /month");
    }
    else
    {
        $(".c3").html("-");
    }
}
}

html code which i added on index2.php

<table border="0" cellpadding="0" cellspacing="0" class="tb1">
    <tr>
        <th width="25%">danger level</th>
        <th width="25%" class="bg1">doctor working time</th>
        <th width="25%" class="bg2">expert's working time</th>
        <th width="25%" class="bg3">duration of other staff</th>
    </tr>
    <tr>
        <td><b>Less dangerous</b></td>
        <td class="a1">0</td>
        <td class="b1">0</td>
        <td class="c1">-</td>
    </tr>
    <tr>
        <td><b>dangerous</b></td>
        <td class="a2">0</td>
        <td class="b2">0</td>
        <td class="c2">-</td>
    </tr>
    <tr>
        <td><b>very dangerous</b></td>
        <td class="a3">0</td>
        <td class="b3">0</td>
        <td class="c3">0</td>
    </tr>
</table>

I also added jquery and js tags and onClick="calculate(); (inside button), but did not work again

How can I run JavaScript on the second page(index2.php) according to the value entered in the first page(index1.php)?

Note:It works fine on one page. But i have to make it dynamic with php(two page) because I will do attachments in the future.

  • Are you putting the value from the employee_number into index2.php in a form field some where? – Taplar Dec 06 '17 at 21:28
  • Prime concepts: 1. a javascript runs in one (html) page. It does not keep anything into any next page. 2. calculations done client side should be validated server side (security) as you cannot trust the client side to actually do what you told it to do. 3. you can do anything in one page if you want to, even uploads etc. (see ajax) 4. if you want to transport something into a next page you need to send it to the server (e.g. using a hidden value in a form) and have the server send it back to your script –  Dec 06 '17 at 21:34

2 Answers2

0

When you submit the form it will load index2.php and the value of employee_number is in

$_GET['employee_number'];

if you want to keep your urls clean you can change the method to post and the vars will be in the $_POST array.

BsdGuru
  • 1
  • 1
0

Put a hidden input where you put your calculated value

document.getElementById('price').value = result
<input type="hidden" name="price">

And you have the price in your php by the name price

Ana Houa
  • 901
  • 1
  • 8
  • 19