Jump to content

Navigating the DOM with php


Fukushousha

Recommended Posts

Hello all,the topic title says it all. I would like to know if it is possible the navigate the html DOM just like I can do with javascipt but with php. The purpose is to get the values of checkboxes from some very big forms with other ways other than going one by one and checking their names with their values. I mean ... isn't there anything equivalent to document.getElementsByTagName("input") of jscript in php? Lastly ... should I do it like that? Or I shouldn't even bother and stick to the one by one way?

Link to comment
Share on other sites

You can't access the page through PHP after it loads - PHP is a hypertext pre-processor (while JS is a post-processor). There are XML tools (e.g. through the DOMDocument() object) but you can't access the user's input through that way (as they enter it after the page loads).

Link to comment
Share on other sites

Ok thanks that's what I wanted to know. I guess I will have to manually type in the $_POST['checkboxname'] here to get all the checkbox values :) .I hoped to have made the code look neater that way. We can't have it all I guess.Actually .... it just struck me. Since I generated all the checkboxes and well all the other input tags in the form by reading some xml files and the names are inside those xml files ... I could read it from the xml with php and do it in a nice little loop!!!

Link to comment
Share on other sites

If it's a big list of checkboxes you might want to put them in an array.<input name="check[]" value="1"><input name="check[]" value="2"><input name="check[]" value="3"><input name="check[]" value="4"><input name="check[]" value="5">If you loop through $_POST['check'] it will contain only the values that they checked.

Link to comment
Share on other sites

Thanks for the tip someguy, I will have it in mind.There is one other problem I stumbled upon and I have absolutely no idea how to solve it.I have it reading the names of the checkboxes from the same XML file my javascripts created the checkboxes. So there should be absolutely no error. Yet I get undefined index errors on all the checkboxes and on 3 radio buttons (out of the 40) which happen to contain empty space.I have no idea why it does not work for the checkboxes, or for those very specific 3 radio buttons. Since I am naming them all from the xml file it should either work for all or not work at all. I will take a break and rework on the whole thing a little later, but if someone has any idea why such a thing would happen please say so.For checkboxes ... if you give them normal names like name="aname" and not arrays like justsomeguy proposed above it still works eh?Why would $_POST['$name'] return undefined index unless the name was wrong? Is there any other reason? I mean .. the name just can't be wrong, the checkboxes and the radio buttons get created and get named by two javascripts functions which read an XML file. It just doesn't make sense to me :)

Link to comment
Share on other sites

If they don't check the box then it doesn't get submitted in $_POST. That's why it's useful to put them in an array and then you're only looping through the selected values instead of needing to loop through everything and checking if it's set. I'm not sure about the radio buttons, you might need to convert spaces to underscores.

Link to comment
Share on other sites

Justsomeguy honestly thank you.I figured it all out. Well it was as simple as that. If a checkbox IS NOT checked even if you put its name a $_POST['checkboxnamehere'] it will be seen as an undefined index. So arrays is not just good practise. It is compulsory. Moreover spaces do indeed get changed to underscores so the radio buttons and the checkboxes which had spaces were confusing me. I noticed it by checking every checkbox and submitting the form, .... in which case suddenly it all went well and an addition to the DB was made. Only then was it that I realized the utter stupidity that has been stalling me all day today.Again thanks are in order. I hope this topic helps other people who might run to the same trouble someday.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...