Hi! I have a php form and I am using it to update,delete and insert data.I use a drop down to display customer names and when the customer is selected, the form should be submitted with document.frm1.submit(); so that I can get company id of that customer.

Problem is, when I try to update the data and select customer,suddenly the insert query is executing.I tried to put conditions, sending hidden values but nothing seems working.Either the form refuses to submit, or submits accidentally due to document.frm1.submit();

Here is part of my code.Please help me in this regard.

<?PHP 
$id=$_GET['id']; 
//query to display database values when update link is clicked 
                    $query=mysql_query("SELECT * FROM  sales WHERE id='".$id."'"); 
                    $row_1=mysql_fetch_array($query); 


if(isset($_REQUEST['action_perform'])) 
{ 


                    $id=$_REQUEST['id']; 
                    $tbl = 'sales'; 
                    $field = 'number'; 
                    $post = $_POST['number']; 
                    $where_clause = "id='".$id."'"; 


                  $insert_array = array('cust_id' => $_POST['name'], 
                                        'co_id' => $_GET['co_id']); 

                    if($_REQUEST['action_perform']=="delete"){ 
                    dbRowDelete($tbl, $where_clause); 
                    header('Location:sales.php?query=delete'); 
                    } 


                    if($_REQUEST['action']=="update"){ 
                    dbRowUpdate($tbl, $insert_array, $where_clause); 
                    header('Location:sales.php?query=update'); 
                    } 

                    if($_REQUEST['action_perform']=="Save"){ 

                    $check = check_duplicate($tbl,$field,$post); 

                    if($check > 0){ 
                    header('Location:sales.php?query=duplicate'); 
                    } 


        else{ 



                            dbInsert($tbl,$insert_array); 

                            header('Location:sales.php?query=insert'); 
                    } 
                                                    } 

        } 
?> 
<script language="javascript"> 
function form_submit(){ 
    document.frm1.submit(); 
} 
</script> 

<form name="frm1" id="frm1" method="post" action=""> 
<table style="margin:10px; padding:10px;" > 
<tr> 
<td class="col-first">Client Name:</td> 
<td> 
<?PHP 
$query=mysql_query("SELECT * FROM new_customer");?> 
<select name='name'  class="required form-select" title="Please select customer name" onChange='javascript:form_submit();'> 
<option value="">Select</option> 
<?php 
while($row=mysql_fetch_array($query)) 
{ 
?> 
        <option value= <?PHP echo $row[cust_id]; ?> <?PHP echo($row[cust_id]==$_POST['name'])||($row[cust_id]==$row_1[cust_id])?'selected':''?>> <?PHP echo $row[cust_name]; ?> </option> 

<?php 
} 
?> 
</select><span class="style1">*</span> 
</td> 
<?PHP     
if(isset($_POST['name'])){ 
        $co_query = mysql_query("SELECT * FROM new_customer,new_company WHERE new_customer.co_id=new_company.co_id AND new_customer.cust_id='".$_POST['name']."'"); 

            $row = mysql_fetch_array($co_query); 
            $co_id = $row['co_id']; 

} 
?> 
</tr> 
<tr> 
<td colspan="6"> 
            <div align="center"> 
       <input type="hidden" name="id" value="<?PHP echo $id; ?>"> 
    <input type="hidden" name="co_id" value="<?PHP echo $co_id; ?>"> 
    <input type="hidden" name="action_perform" value="<?php echo ($_REQUEST['action']=="update") ? 'Update' : 'Save'; ?>"> 
      <input name="Submit" type="submit" value="<?php echo ($_REQUEST['action']=="update") ? 'Update' : 'Save'; ?>" class="search_btn"/> 
      <input type="reset" value="Cancel" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'];?>'" class="search_btn">  
</div> 
            </td> 
            </tr> 
</table> 
</form>

Dani AI

Generated

As described, the immediate symptom is exactly what pointed out: the select's change handler forces a full form submit, so the same request-processing code that handles Save/Update/Delete runs as soon as a name is picked. 's idea of separating processing and display logic is valid, but there are a few safe, practical options that avoid accidental DB writes and still let the UI fetch the company id.

A simple server-side guard (preferred quick fix) is to only run insert/update when the actual submit button was used. Programmatic submits (form.submit()) do not include the clicked button value, so checking for it prevents accidental operations:

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['Submit'])) {
  if ($_POST['Submit'] === 'Save') {
    // insert
  } elseif ($_POST['Submit'] === 'Update') {
    // update
  }
}

If the goal is to populate co_id immediately when a customer is chosen but avoid a full submit, use an AJAX lookup instead of submitting the whole form. Example (vanilla fetch):

document.getElementById('name').addEventListener('change', function() {
  var id = this.value;
  if (!id) return;
  fetch('get_company.php?cust_id=' + encodeURIComponent(id))
    .then(r => r.json())
    .then(data => {
      document.querySelector('input[name="co_id"]').value = data.co_id || '';
    });
});

and a tiny server endpoint returns JSON for the cust_id (use prepared statements and proper validation).

Other important notes: remove the automatic submit if it is not wanted; if the submit must happen, set a distinct hidden flag only when the real Save/Update button is clicked; keep processing code in a separate script or clearly gated by intent checks; and migrate away from deprecated mysql_* functions—use PDO or mysqli with prepared statements and always validate/sanitize inputs and add CSRF protection.

Recommended Answers

All 5 Replies

Try to keep separate login for updation and html form. so upto line 53 keep code in process.php
later part keep in same file.
In format action call process.php

Try to keep separate logic for updation and html form. so upto line 53 keep code in process.php
later part keep in same file.
In form action, call process.php

<select name='name' class="required form-select" title="Please select customer name" onChange='javascript:form_submit();'>

the form should only be submitting when you change the option on the select box, are you supposed to be able to enter data after selecting it? if you are this method won't work, you should just use the submit button

actually when i hit the update link and select customer name from dropdown, suddenly the form submits and "data updated " message displayes.i am not sure keeping code separate will solve the issue.but let me check it once.

actually when i hit the update link and select customer name from dropdown, suddenly the form submits and "data updated " message displayes.

thats correct you've told it to submit when you pick a name : onChange='javascript:form_submit();'

<select name='name'  class="required form-select" title="Please select customer name" onChange='javascript:form_submit();'>

Remove that onchange if you don't want it to

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.