Jump to content

PHP help for noob


catague

Recommended Posts

If you're looking to have it automatically add up without a page refresh or submit button clicked you'd need to use Javascript & AJAX.

 

Otherwise your form could look like this:

<form method="post" action="index.php">
<input type="text" name="field_1" /> First Number<br />
<input type="text" name="field_2" /> Second Number<br />
<input type="submit" />
</form>

 

on the index.php file in the header you would do this:

<?php

$val = $_POST['field_1'] + $_POST['field_2'];
echo $val;

?>

Link to comment
Share on other sites

Depends what you mean by "automatic". You would want to use Javascript if you want to automatically add the numbers together without the user clicking a button to submit the form. You would need to use Javascript's onChange event to watch for when the values in the input's change, and automatically do the calculation once both inputs have a value.

 

If you are using PHP with a submit button, you'd need to create a form that allows users to enter numbers. When the form submits, you'd want to use $_POST[] to get the values that the visitor submitted, do the addition, and then display the calculated results back to the user.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...