0

When I'm passing a form id to JavaScript function, the actual value what is passed there is not a string but [object HTMLFormElement]. Here is the code of a JavaScript:

function hideSowDiv(id) {
    alert(id);
    if (document.getElementById(id).style.display == "none") {
        document.getElementById(id).style.display = "block";
    } else {
        document.getElementById(id).style.display = "none";
    }
}

and it is a php part of the code:

while($ProductList = mysql_fetch_array($GetUers)){
    echo "<div >";
    echo "<input type='submit' name='OpenBtn'  value='+' onclick='hideSowDiv(ItemUnit".$ProductList[0].")' > &nbsp; ";
    echo "<span >".$UserList[4]." </span>";
    echo "</div>";
    //echo "<div id='".$UserList[0]."' style='display:none;>";
    populateUserDivs($ProductList[0]);
    //echo "</div>";
}

function populateUserDivs($Id){
    $List_Query = "SELECT * FROM product WHERE product_id = ".$Id.";";
    $GetList = mysql_query( $List_Query, $Connection ) or die("ERROR".mysql_error());
    while($Output_List = mysql_fetch_array($GetList))
        echo "<form  id='ItemUnit".$UserId."' name='ItemUnit".$UserId."' method='POST' style='display:none;' >";
        echo "<span class='AddingForm' > Name*: &nbsp; </span> <input type='textbox' name='UserName' class='Text_boxes' value='".$Output_List[1]."' required> &nbsp;&nbsp;&nbsp;";;
        echo "<input type='submit'  name='EdtItem".$Output_List[0]."' id='DelItem".$Output_List[0]."' value='save'  '>
        echo " </form>";
    }
}

So I wonder what am I doing wrong that the form id is not being passed to the JavaScript function, but the [object HTMLFormElement] instead.

The reason I'm passing the form id is if I put the form inside a div, the submit button is being treated just like an image and form is not working at all.

So if somebody can point me how can I turn the [object HTMLFormElement] into the form id I will be really grateful.

Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35
mister sweet
  • 53
  • 1
  • 9
  • You'll need to create an [MCVE](/help/mcve) using just the HTML output (not the PHP), since you've said the obvious answer (putting the `id` in quotes in the `onclick`) didn't work. – T.J. Crowder Jun 30 '15 at 07:53
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 30 '15 at 09:38
  • ...and don't use inline javascript, and probably you want to trigger the "focus" event instead of "click", and you shouldn't use ` ` for layout purposes, and you shouldn't use `mysql_`, and it looks strange to me to have a control name like "editItem" together with its id being "DelItem",... In other words, just a few line of codes, but a lot of bad-practices in it, with a impact from very small to possibly very large... – Laurent S. Jun 30 '15 at 09:40

0 Answers0