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>

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.