Hend
Hend

Reputation: 599

Button in html javascript not working

I am trying to submit a form to execute a php script. But the submit button is not working. It doesn't seem like I'm missing any quotes or expressions. What may be the problem I am doing wrong?

<script type="text/javascript">

function doSubmit()
{

<?php
echo "Hello";
?>
var actions = document.frm.actions.value;
var tables = document.frm.tables.value;
var text0 = document.frm.text0.value;
var text1 = document.frm.text1.value;
var text2 = document.frm.text2.value;

/*var txtBox = document.getElementById("text3");

if (txtBox!=null){
var text3 = document.frm.text3.value;
var strURL = "devicesql.php?actions=" +actions+ "&tables=" +tables+ "&text0=" +text0+     "&text1=" +text1+ "&text2=" +text2+ "&text3=" +text3;
}

else
{*/
var strURL = "devicesql.php?actions=" +actions+ "&tables=" +tables+ "&text0=" +text0+ "&text1=" +text1+ "&text2=" +text2;
//}

var xmlhttp;

if (str=="")
  {
  document.getElementById("result").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
  }

xmlhttp.open("GET",strURL,true);
xmlhttp.send(null);

}

</script>

<form name="frm" method="get" action="doSubmit();">


<?php

// Connect to server and select database.
 mysql_connect("127.0.0.1:3306", "root", "")or die("cannot connect"); 
 mysql_select_db("PushApplication")or die("cannot select DB");

$sql="SELECT * FROM Device";
 $result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
 <table width="400" border="0" cellspacing="1" cellpadding="0">
 <tr>
 <td><form name="form1" method="post" action="">
 <table width="400" border="0" cellspacing="1" bgcolor="#CCCCCC">

 <tr>
 <td align="center" bgcolor="#FFFFFF">#</td>
 <td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>DeviceID</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>DeviceType</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>OS_Version</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>Status</strong></td>

 </tr>
<?php
 while($rows=mysql_fetch_array($result)){
 ?>
 <tr>
 <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['ID']; ?>"></td>
 <td bgcolor="#FFFFFF"><? echo $rows['ID']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['DeviceID']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['DeviceType']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['Description']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['OS_Version']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['Status']; ?></td> 
 </tr>
<?php
 }
 ?>
 <tr>
 <td colspan="7" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
 </tr>
<?php
// Check if delete button active, start this
 if($delete){
 for($i=0;$i<$count;$i++){
 $del_id = $checkbox[$i];
 $sql = "DELETE FROM Device WHERE ID='$del_id'";
 $result = mysql_query($sql);
 }

// if successful redirect to delete_multiple.php 
 if($result){
 echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
 }
 }
 mysql_close();
 ?>
 </table>
 </form>
 </td>
 </tr>
 </table>

<br />
<br />


Action: <id="actions">
<select name="actions">
<option value="insert into ">Insert</option>
<option value="update">Update</option>
<option value="select">Display</option>
</select>

</br>
</br>

Database Table Name: <id="tables">
<select name="tables" onchange="addRows(this.value)">
<option>Select Table</option>
<option value="Applications">Application</option>
<option value="Device">Device</option>
</select>
</br>

<div id="addedRows"></div>

</br>

<input type="submit" value="Submit" onclick="doSubmit()">

</br>
</br>

Result: <div id="result"></div>

</form>

Upvotes: 1

Views: 321

Answers (3)

J eremy
J eremy

Reputation: 39

Try to create another form for the table displaying the database information. Your form might be confused with the 2 buttons.

Upvotes: 0

Ket.
Ket.

Reputation: 774

use following code to submit the form instead of submit button,

<a href="javascript: doSubmit()">Submit</a>

Upvotes: 0

Andr&#233;s Torres
Andr&#233;s Torres

Reputation: 733

You need to remove the action attribute from the form, and the input that submits the form should have an onclick attribute that fires your function and not the submit event.

<form id="theform">
    <!-- some input texts.... or something -->
    <button onclick="processForm()">Process form</button>
</form>

<script type="text/javascript">

    function processForm () {
        var form = document.getElementById('theform');

        // your stuff
    }

</script>

PS: You are creating a sintax error with the first PHP tag that echo "Hello"

Upvotes: 1

Related Questions