I have a JQuery autosuggest working properly on my form.php page... Now I want to get my form to actual POST the input values. So currently if I just run search.php, I get the following (using echo json_encode):

{"label":"Henry Gale","value":"henrygale@gmail.com"},{"label":"Amy Gerges","value":"amy@yahoo.com"}, and the list goes on.

So since the search is working properly. Now, I want to POST only the values that I place in the form's input field. I currently have:

<script>
	$(document).ready(function() {
		$( "#autocomp" ).autoSuggest("search.php", {
			minChars: 2, 
			selectedItemProp: "label", 
			searchObjProps: "label", 
                        selectedValuesProp: "value",
			selectionLimit: 8, 
			formatList: function(data, elem){
				var my_image = data.image ;
				var my_label = data.label;
				var new_elem = elem.html(my_image + my_label);
				return new_elem;
			}
		});
	});
</script>

<input name="contacts" type="text" id="autocomp" maxlength="35" />

But if I do an echo of the $_POST['contacts'] I just get the word: Array

I am doing something wrong, just not sure what... Since my input gets a list of comma separated values, how can I:

1) make sure the input values get the "value" attribute which corresponds to the emails.
2) post the emails so I can do things with the emails (run them through format checks, insert them into a mysql db, etc).

    Have you looked at what the browser has in the contacts field, and what it's submitting to the server? If that looks right then something is happening at the server end and it's that which we need to see.

      Thanks, I did some inspecting on the browser side and here's what I found.

      Ok I figured out that the AutoSuggest script goes back to the AutoSuggest.js file which creates a hidden input field that looks like:

      var values_input = $('<input type="hidden" class="as-values" name="as_values_'+x+'" id="as-values-'+x+'" />');
      [\CODE]
      
      That gets put into the browser html, and the email (value)'s I need, get stored in: as_values_'+x+'
      
      The 'x' is a randomly generated number (which ensures there will be unique IDs on the page if autoSuggest() is called multiple times).
      
      So now if I run:
      [CODE]
      if (isset ($_POST['d_contacts'])){
      	$d_contacts = $_POST['as_values_055'];
      	echo $d_contacts;
      }
      [\CODE]
      
      I get what I need: the comma separated emails. But if I do:
      
      [CODE]
      if (isset ($_POST['d_contacts'])){
      	$d_contacts = $_POST['as_values_'+x+''];
      	echo $d_contacts;
      }
      [\CODE]
      
      I get nothing... How can I retrieve that random number, so that I can make the if(POST) statement work?

        For one, [man]array_keys/man will tell you what keys exist in the $_POST array.

        However, if $_POST['contacts'] has an array of the values you want, why not simply work with that [man]array[/man] instead? Have you examined what is in that array (e.g. using something like [man]print_r/man or [man]var_dump/man)?

          I think $POST['d_contacts'] only runs because that form is getting posted, but the values I need are only contained in the hidden input field, which holds as_values'+x+' (where x is the random number generated in the .js file).

          So, I did a dump:

          if (isset ($_POST['d_contacts'])){
          	var_dump($_POST['d_contacts']);
          	var_dump($_POST['as_values_092']);
          	var_dump($_POST['as_values_'+x+'']);
          	exit();
          }
          

          and here's what I get:
          string(0) " "string(22)" useremail@gmail.com," NULL

          So obviously the second variable with the 092 the randomly generated number, works BUT that's only because I peeked at it in the browser and manually put it in there.

          So how am I supposed to get the random number into the POST (so I can run some checks like email format, mysql real escapes, etc...).

            You can either do as I said before and use [man]array_keys/man to get an array whose values are the keys of the $POST array, or you could instead simply use a [man]foreach/man loop over the entire $POST array.

              I think you may be misunderstanding or I'm not explaining it right... my problem is not getting the values from the array, my problem is trying to get this: $POST['as_values'+x+''] to work like this $_POST['as_values_092] ...

              I need to find a way to get the value of that random number in hidden field:

              <input type="hidden" class="as-values" name="as_values_'+x+'" id="as-values-'+x+'" />

              so I can run my POST (as opposed to having to scrape the random number using firebug to inspect the element).

                For the third time, [man]array_keys/man will give you an array of keys found in the $POST array. You can simply loop through that array until you find an element that fits the pattern of the string 'as_values' followed by a series of numbers.

                Then again, I'm not sure why the exact values of those numbers matter at all if you can simply use a [man]foreach[/man] loop on the $_POST array and simply use the value contained at that index.

                  Write a Reply...