1

I would like click on submit and the value input in the field to be stored in database.

However, I do not want to use a form action. Is it possible to do it without creating form action with PHP?

<tr>
<form method="post">
<tr>
    <td>
        <label for="Item name"><b>Finish Product:</b></label>
    </td>
    <td>
        <input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
    </td>
</tr>
<tr>
    <td>
        <input type="submit" value="Save" id="submit" />
    </td>
</tr>

<?php
if(isset($_POST['submit']))
{
var_dump($_POST); exit;

$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
4
  • You can do it by posting the form with AJAX... Commented Jan 13, 2016 at 12:05
  • I have no knowledge on AJAX, may I ask how to do it? Commented Jan 13, 2016 at 12:06
  • basically you doesn't want form action but you want pass data into post method ....? Commented Jan 13, 2016 at 12:12
  • check my updated post Commented Jan 13, 2016 at 12:12

2 Answers 2

1

Use Jquery ajax to do this. Try this: HTML

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<body>
<table>
<tr>
<td><label for="Item name"><b>Finish Product:</b></label></td>
<td><input id="finish_product" type="text" maxlength="100" style="width:100px" name="finish_product" required></td>
</tr>
</table>
<input type="button" value="Save" id="submit" />
    <script>
    $(document).ready(function(){
       $('#submit').click(function(){
          $.ajax({
                url: '1.php',
                data: {finish_product: $('#finish_product').val()},
                success: function (result) {
                    alert(result)
                }
            });
       });
    });
    </script>
</body>

PHP (1.php)

Note: Use mysqli_query since mysql_query is depricated in latest versions. And use bind param instead of directly appending values to query.

<?php
if (!empty($_GET)) {
    $SQL = "INSERT INTO bom (finish_product) VALUES ('".$_GET['finish_product:']."')";
    $result = mysql_query($SQL);
    echo 1;
} else {
    echo -1;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0
  1. This isn't a form, this is just a series of table elements with various inputs and a submit button. Clean up the code - there's no closing tr tag, and where is the submit button supposed to be?
  2. Add a form element around it.
  3. You need an method attribute to the form - in this case, "post". Without the action attribute, it will default to the same page.

<!-- add form tag -->
<form method="post">
    <tr>
        <td>
            <label for="Item name"><b>Finish Product:</b></label>
        </td>
        <td>
            <input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
        </td>
    </tr>
    <!-- added new row for submit button - you might want to have the td element span two columns? -->
    <tr>
        <td>
            <input type="submit" value="Save" id="submit" />
        </td>
    </tr>
</form>
<-- end of form -->

<?php
if(isset($_POST['submit']))
{
    //see what is being POSTED - comment out this line when you're happy with the code!
    var_dump($_POST); exit;

    $SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
    $result = mysql_query($SQL);
}

1 Comment

Hi, I have tried but nothing is added to my database after I input Table at the field. Why is this happening?

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.