1

The form that I'm trying to work has two buttons: 1: for viewing the submitted information and 2: for saving the confirmed information.

Part of my form:

    $sql="INSERT INTO applicant_information
    (entrepreneur_name,enterprise_name,.....) values 
    ('".$_POST['entrepreneur_name']."','".$_POST['enterprise_name']."','".$_POST['address']."'...)


     <form method="post" action="business_form.php">
     <table width="70%" cellspacing="2px" cellpadding="5px"style="border:1px solid   black;border-collapse:collapse;">
      <th colspan="8"align="left" style="border:1px solid black;"><b>Personal  

Information</th>
  <tr>
    <td width="18" rowspan="2" style="border:1px solid black;">1</td>
    <td width="142" rowspan="2"style="border:1px solid black;" >Name</td>
    <td style="border:1px solid black;" colspan="2">Entrepreneur</td>
    <td colspan="2"style="border:1px solid black;"><?php echo $_POST['entrepreneur_name']?>
    <input id="entrepreneur_name" name="entrepreneur_name" type="hidden" value="<?php echo $_POST['entrepreneur_name']?>" />
    </td>
  </tr>.....
    //rest of the form

    <input type="submit" name="edit" style="width:10%"value="Back to edit" />
    <input type="submit" name="reg"style="width:10%"value="Submit" />

What I'm trying to do is to run the query when the user hit the submit button. Any idea how to do that?

Mar Far
  • 27
  • 1
  • 1
  • 6
  • use if condition with submit button name may this helps you – keshu_vats Sep 12 '13 at 03:58
  • Changing the form action is actually pretty common. Take a look at this similar question: http://stackoverflow.com/questions/11368000/javascript-change-form-onsubmit-dynamically – Chris Rasco Sep 12 '13 at 03:59
  • Just a note on your query, presuming it is how you intend to use it. You may want to avoid using results from the _POST array directly in the query. You leave yourself open to SQL-Injection and other fun problem. :) – David 'the bald ginger' Sep 12 '13 at 06:07

4 Answers4

1

What I usually do is just have one button change the form's destination on click, then submit it. So for example:

<form action="login.php" method="POST" id="myform">
    <input name="username">
    <input type="password" name="password">
    <input type="submit" value="Login">
    <button id="js-register">Register</button>
</form>

With

$('#js-register').click(function() {
    $('#myform').attr('action', 'register.php').submit();
});

Or you could have both buttons be Javascript'd and bind both of them for consistency's sake - up to you.

Connor Peet
  • 6,065
  • 3
  • 22
  • 32
0

HTML

<form action="handle_user.php" method="POST />
<input type="submit" value="View" name="view" />
<input type="submit" value="Submit" name="submit">
</form>

Check condition in php as following way...

if($_POST["view"]) {
  //User hit the view button, handle accordingly
}

if($_POST["submit"]) {
  //User hit the Submit information , handle accordingly
}
jaydeep namera
  • 1,024
  • 7
  • 15
0

You need to track the button named "reg". So right after the $sql string, you can put the following:

<?php
    if (isset($_POST['reg'])) {
        mysql_query($sql);
        if (mysql_affected_rows() > 0) {
            echo "REgistration completed";
        }
        else {
            echo "System could not process the registration";
        }
    }
?>

Hope that will help you.

0

You could make the "edit" be a plain button, instead of a submit type. And bind a click event to it, which could either redirect to the editable form or make the form editable (which ever suits you best). Then the "reg" submit could work as it does currently to save the data.

David 'the bald ginger'
  • 1,296
  • 3
  • 20
  • 38