Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

multiple select validation with php/jscript

  • 31-05-2006 12:06am
    #1
    Closed Accounts Posts: 8,478 ✭✭✭


    Recently I asked about how the hell to handle multiple select lists with PHP and Javascript. I wanted to perform some validation with Javascript, and some SQL with the form results on the PHP side.

    PHP will handle the multiple select list if you append a [] to the end of the formname, such as
    <select name="mylist[]" multiple>
    

    But if I wanted to do some validation on that, Javascript wouldn't recognise the [] in the name, and would complain accordingly. I googled and googled and scratched my nads when finally I did this
    <select name="mylist[]" id="mylist" multiple>
    

    Note how the ID parameter does not have the brackets. Turns out PHP would take the NAME parameter for processing, and Javascript would accept the ID parameter with a simple this.form.mylist.value blah blah blah.

    Hopefully this might save someone some time in the future. It's one of those dumbass 2 second things that takes way too long to discover.


Comments

  • Closed Accounts Posts: 1 rajatQ2


    Recently I asked about how the hell to handle multiple select lists with PHP and Javascript. I wanted to perform some validation with Javascript, and some SQL with the form results on the PHP side.

    PHP will handle the multiple select list if you append a [] to the end of the formname, such as
    <select name="mylist[]" multiple>
    

    But if I wanted to do some validation on that, Javascript wouldn't recognise the [] in the name, and would complain accordingly. I googled and googled and scratched my nads when finally I did this
    <select name="mylist[]" id="mylist" multiple>
    

    Note how the ID parameter does not have the brackets. Turns out PHP would take the NAME parameter for processing, and Javascript would accept the ID parameter with a simple this.form.mylist.value blah blah blah.

    Hopefully this might save someone some time in the future. It's one of those dumbass 2 second things that takes way too long to discover.

    Hi Goneshootin,
    Trying your code out:
    <select size="18" name="fromlist[]" id="fromlist" multiple >
    
    and on the action page:
    
    	$list1VAR = $_POST['fromlist'];
    	echo "Got something in list1: $list1VAR[0].<br>\n";
    

    This doesn't work. The var comes back empty. Seems as if JS does recognize the ID tag, but perhaps PHP is using that too and not posting the variables array. Is there anything special i should be doing?

    Cheers


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    The http protocol which php et al depend upon to get form input, will look at the form elements name and value. I don't think http can give anything else back to the server script.

    Javascript on the other hand runs in the browser, not on the server, and has access to the document object model, which is anything and everything that appears in the document that is returned to the browser, including everything the person does with the web page.

    Loooks to me like javascript is not taking the square brackets as part of the element name, maybe as part of its own syntax, leading to a parse 'error'. But the hack of using id - a css identifier, as another way to isolate the form element you want to address works.

    You have to get inside the head of the program you're trying to control.


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    rajatQ2 wrote:
    and on the action page:
    	$list1VAR = $_POST['fromlist'];
    	echo "Got something in list1: $list1VAR[0].<br>\n";
    
    foreach ($_POST[fromlist] as $array_value => $value)
    	{
    		$insertQuery = "INSERT INTO something (id, some_id, site_id) VALUES(null,'some_id', '$value')";		
    
    		$insertAction = mysql_query($insertQuery ) or die("SELECT Error: ".mysql_error());		
    	}
    

    That may answer your question rajatQ2


Advertisement