0

I have a from that PHP builds with and jQuery and ajax retrieve to the current page. The problem now is that all of the input elements have the same names and Id's. The forms do have unique ID's. How to I point to the unique form then to the input ID's that have duplicates?

All I need is something like document.FormId.inputID.value() So that I can get data from specific forms as the inputs themselves share Ids

Here is the code that the PHP uses:

while ($row = mysqli_fetch_array($result)) {
   $Output .= 
"<tr>".
'<form name="'.$row['EmailID'] .'"  action="" method="post">'.
"<td>" .
'<input name="Email" type="email" id = "Email" value="'.$row['Email'].'">'                      ."</td>"."<td>".
'<input name="FName" type="text" id = "FName" value="' . $row['FName'] .
'">' .
"</td>"."<td>" .
<input name="LName" type="text"  id = "LName" value="' . $row['LName'] .
'">' . "</td>".
//this hidden value passes the Email ID with the form
'<input id="EmailID" type="hidden" value="' . $row['EmailID'] . '" name="EmailID">'.

"<td>" .
'<input type="button" class="button" name = "Edit" id="Action" value = "Edit"  onclick="validateInput('.$row['EmailID'].')  />' 
. "</td>".
"<td>" 
. '<input type="button" class="button" name = "Delete"  id="Action"   value="Delete"
 onclick="validateInput('.$row['EmailID'].');" />'. "</td>".
"</form>".
"</tr>";
}   
echo $Output;

here is a form that it produces:

<tr>
<form name="21" action="" method="post">    <td><input name="Email" type="email" id="Email" value="[email protected]"></td>
<td><input name="FName" type="text" d="FName" value=""></td>
<td><input name="LName" type="text" id="LName" value=""></td>
<input id="EmailID" type="hidden" value="21" name="EmailID">
<td><input type="button" class="button" 
name="Edit" id="Action" value="Edit" onclick="validateInput(21) /></td>
<td><input type=" button"=""></td>
</form>

</tr>

Now here is the old JS that would pull the values then later use them through ajax. Right now this is broken and not working, but you can see how it was working.

function validateInput(RowId) { 


$(function() {
    $('.Action').click(function(event) {
        alert('Button ' + String(event.target.value) + ' was clicked!');
    });
});

var Action = document.getElementById("Action").value;
alert(Action);
if(Action == 'Delete'){
    var EmailID = document.getElementById("EmailID").value;
    var Email = document.getElementById("Email").value;
    alert("'Action == 'Delete'" + EmailID);


    $.ajax({
        url: "ProEmail.php",
        data: { Action: Action, EmailID: EmailID, Email:Email },
        type: "POST",
        success : function (data) {
            LoadTables();                       
        }
    });
    return null;
    }


var Fname = document.getElementById("FName").value;
var Lname = document.getElementById("LName").value; 
8
  • ids MUST be unique, that is your first issue Commented Jun 23, 2014 at 14:12
  • @SamCreamer ... thank you but I did have it working just fine a different way with duplicut ids. I do want to see it it will work by using the form names then the element Ids. Commented Jun 23, 2014 at 14:17
  • yes, you can make it work using names, classes, etc, but IDs should still be unique. EDIT: otherwise what's the point of an ID? Commented Jun 23, 2014 at 15:02
  • 1
    Using duplicated IDs doesn't explicitly invalidate your HTML, but will almost certainly cause a lot of problems if you ever want to refer to them with javascript (which you do). Commented Jun 23, 2014 at 15:11
  • 1
    I see, in that case, maybe use classes. Commented Jun 23, 2014 at 15:23

1 Answer 1

1

Since your form has a name attribute, you can access the particular form using javascript by using document.formName. In your case document.21 should work.

Sign up to request clarification or add additional context in comments.

7 Comments

Thankyou, Ill give that a shot. document.forms[formName].elements["InputID"].value; would that be it?
Cannot read property 'elements' of undefined on this line var test = document.forms[RowId].elements[FName].value;
This should be a working example in your case: document.forms[0].elements['FName'].value. The quotes are important
Use document.forms[0] to reference your first form and subsequent numbers for subsequent forms. rowId wont work as a good reference because it's a number and you're dealing with arrays and collections. Sorry if this is confusing or not helping you but I have tested this and it works for me.
So I would need to add reference numbers for the input IDs anyway wouldn't I?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.