1

i would like to ask if it is possible to get a value from a html form without submitting the form? i cant get this to work in html or php, or maybe i'm missing something. please help. see my working code below:

<FORM  name="myForm" action="confirmsold.php" method="post" onsubmit="return validateFormLay()">
    <table border=0 width="550" cellspacing=1 cellpadding=3 bgcolor="#353535" align="center">
<tr>
        <td bgcolor="#ffffff">Price Basis:</td>
        <td bgcolor="#ffffff">
        <?php
        $connect = mysqli_connect("localhost","root","ilovegym") or die(mysqli_connect_error());

        mysqli_select_db($connect, "laborsa") or die(mysqli_error());

        $sql = "SELECT Cash_Price,3months,6months,12months,18months FROM itemmaster where Item_Number='".$_SESSION['ITEM#']."'";
        $result = mysqli_query($connect,$sql);

        echo "<select name='basis'>";
        while ($row = mysqli_fetch_array($result)) {
        echo "<option value='" . $row['Cash_Price'] ."'>" . $row['Cash_Price'] ." - ".CashPrice."</option><option value='" . $row['3months'] ."'>" . $row['3months'] ." - ".ThreeMonths."</option><option value='" . $row['6months'] ."'>" . $row['6months'] ." - ".SixMonths."</option><option value='" . $row['12months'] ."'>" . $row['12months'] ." - ".TwelveMonths."</option><option value='" . $row['18months'] ."'>" . $row['18months'] ." - ".EighteenMonths."</option>";
        }
        echo "</select>";
        ?>  
        </td>
    </tr>

    <tr>
        <td bgcolor="#ffffff">Downpayment:</td>
        <td bgcolor="#ffffff"><INPUT type="TEXT" name="downpayment" size="12" maxlength="11" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;"></INPUT></td>
    </tr>

    <tr>
        <td bgcolor="#ffffff">Balance:</td>
        <td bgcolor="#ffffff"><INPUT type="TEXT" name="laybalance" size="12" maxlength="11" onkeypress="if ( isNaN( String.fromCharCode(event.keyCode) )) return false;"></INPUT></td>
    </tr>

as you can see, i need to choose from the drowdown a payment basis, then ill input a downpayment. how can i make the balance field show the balance automatically? i only need to subtract the basis-downpayment, difference will be put in the balance field.

Please help anyone. Thank you.

2 Answers 2

1

Client-Side vs Server-Side

PHP works purely on the server side - you can't get the value of a input element without submitting the form using PHP alone. However, you can do this using Javascript - this runs on the client side, rather than the server side - it is executed on the user's computer, rather than the server. This means it can run while they are on the page. Javascript can get the value of a form element for you.

Javascript Code

Like PHP needs the name attribute to figure out which input field contains what information, Javascript needs the id attribute to get the value of an input field. First, here's the HTML code:

<input type="text" id="myAwesomeIdName">

Now, you need to "get" the element - this returns the HTML element as an object that Javascript can work with. This can be done using the function document.getElementById(id), where id is the id of the HTML element. Example:

document.getElementById("myAwesomeIdName")

All that we need to do now is get the value attribute of the input - this is done like so:

document.getElementById("myAwesomeIdName").value;

And that returns the value of the input box.

Putting it together

Here's some code which pops up an alert displaying the contents of an input box:

<input type="text" id="myAwesomeIdName"><br><button onclick="alert(document.getElementById('myAwesomeIdName').value)">Show Value</button>

Sign up to request clarification or add additional context in comments.

Comments

0

You need Javascript for this. Give the three elements (<select>, <input> 1 and <input>2 ) also an id="yourid", is somewhat more comfortable writing your javascript code.

Make your second <input> field read-only as the value is a result of a calculation and not a user input.

Write your function in javascript, and let the function be executed when the downpayment is entered, something like oninput=mycode(). But let the function be executed also when the user changes the <select>, something like onchange=mycode().

The javascript function does this (sorry, no code but procedure..)

  • get the value of the <select> event the user has chosen

  • get the value of the first <input> field

  • subtract those values and write the result in the second read-only <input> field

Some considerations:

  • better let the first <option> </option> show 'Select your method' or something like that, with a zero value attached to it. Then you can be sure the function will be triggered by the user.

  • your use of the <input> element is incorrect. It should not have a closing tag. </input> is incorrect html as far as I know.

  • somewhat beside the question, but <td bgcolor="#fff"> better can be <td style="background:#fff">

When the values are OK the user can submit the form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.