Document.frm1.submit(); submitting form directly

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.gr.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>


Shouldn’t “document.gr.submit();” be “document.frm1.submit();”

I am sorry for that.It is actually document.frm1.submit(); I copied wrong code from another file.

I dont see an Insert query anywhere in there?

EDIT: Okay, i see where you’re referring to.
You’re aware that you do this:
<input type=“hidden” name=“action_perform” value=“<?php echo ($_REQUEST[‘action’]==“update”) ? ‘Update’ : ‘Save’; ?>”>

right?

I am using insert function to insert query.Yes I changed the values like value=“<?php echo ($_REQUEST[‘action’]==“update”) ? ‘Update’ : ‘’; ?>” and checked if action_perform==“Update”.But nothing seemed working :frowning:

Your recieving php code is looking for

[FONT=Courier New][COLOR=#007700] 
if([/COLOR][COLOR=#0000bb]$_REQUEST[/COLOR][COLOR=#007700][[/COLOR][COLOR=#dd0000]'[SIZE=4][I][B]action[/B][/I][/SIZE]'[/COLOR][COLOR=#007700]]==[/COLOR][COLOR=#dd0000]"update"[/COLOR][/FONT][COLOR=#007700][FONT=Courier New]){ [/FONT]
[/COLOR]

But your form is sending


[FONT=Courier New]<input type="hidden" name="[SIZE=4][I][B]action_perform[/B][/I][/SIZE]" value="[COLOR=#0000bb]<?php [/COLOR][COLOR=#007700]echo ([/COLOR][COLOR=#0000bb]$_REQUEST[/COLOR][COLOR=#007700][[/COLOR][COLOR=#dd0000]'action'[/COLOR][COLOR=#007700]]==[/COLOR][COLOR=#dd0000]"update"[/COLOR][COLOR=#007700]) ? [/COLOR][COLOR=#dd0000]'Update' [/COLOR][COLOR=#007700]: [/COLOR][COLOR=#dd0000]'Save'[/COLOR][COLOR=#007700]; [/COLOR][COLOR=#0000bb]?>[/COLOR]"
[/FONT]

and since it would seem youre setting $_REQUEST[‘action’] in another part of your script we cant see the update is being triggered.

yes you are right Mandes.for now i have moved edit part to another file.but would like to know if there is any better solution.

The solution was to change

if($_REQUEST['action']=="update"){ 

to


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


may be.for time being solved the issue by

<option value= "<?PHP echo $row1['cust_id']."-".$row1['co_id'] ?>"> 

thanks for your support :slight_smile: