dflow Posted December 6, 2011 Share Posted December 6, 2011 i want to explode a text field on the fly ? for example city,region.country and to turn it to city_id,region_id,country_id 1.textfield from autocomplete 2.get post variable 3.explode 4 get ids i played around with some ajax with php <html> <body> <input name="searchField" id="searchField" type="text" value="<?php echo $_POST['searchField'];?>"/> <script type="text/javascript"> function Ajax(){ var xmlHttp; try{ xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari }catch (e){ try{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer }catch (e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ alert("No AJAX!?"); return false; } } } xmlHttp.onreadystatechange=function(){ document.getElementById('ReloadThis').innerHTML=xmlHttp.responseText; var t=setTimeout('Ajax()',1000); clearTimeout(t); } var searchfieldvalue=encodeURIComponent(document.getElementById("searchField").value) xmlHttp.open("GET","getid.php?searchField="+searchFieldvalue+"",true); xmlHttp.send(null); } window.onload=function(){ var t= setTimeout('Ajax()',1000); } </script> <?php /*explode searchField and generate ids $searchField=$_POST['searchField']; $p = explode(" , ", $searchField, 3); $cityname=mysql_real_escape_string($p['0']); $regionname=mysql_real_escape_string($p['1']); $countryname=mysql_real_escape_string($p['2']); */ ?> <div id="ReloadThis">Default text</div> </body> </html> //getid <?php $p=array(); $p = explode(" , ", $_GET['searchField'], 3); echo $cityname=$p['0']; echo $regionname=$p['1']; echo $countryname=$p['2']; echo'<input type="hidden" name="place" id="place" value="'.$cityname.'"/> <input type="hidden" name="region" id="region" value="'.$regionname.'"/> <input type="hidden" name="country" id="country" value="'.$countryname.'"/>'; here ill add function to get city_id() etc ?> Quote Link to comment https://forums.phpfreaks.com/topic/252607-how-to-explode-on-the-fly/ Share on other sites More sharing options...
WTFranklin Posted December 6, 2011 Share Posted December 6, 2011 Hey, I was just taking a look at this and it seems like that should be working. If you're having any trouble it's could be because of the white space you're passing as a part of the deliminator $p = explode(" , ", $_GET['searchField'], 3); Try it like this and see if it helps: $p = explode(",", $_GET['searchField'], 3); and if you want to ensure that you aren't getting any additional white space in your strings you could put that in your variables like echo $cityname=trim($p['0']); echo $regionname=trim($p['1']); echo $countryname=trim($p['2']); Maybe not the most elegant way, but it could be a start. I'm not really sure where you want to go from there, but I'm assuming you might want to perform a search query or store the data in some way if you're using ajax with that as well. I'm not well-versed enough in ajax, but I know jquery has a nice ajax funtion that maybe could make life a little easier. It did for me anyway. -Frank Quote Link to comment https://forums.phpfreaks.com/topic/252607-how-to-explode-on-the-fly/#findComment-1295042 Share on other sites More sharing options...
xyph Posted December 6, 2011 Share Posted December 6, 2011 Why not just do it all in JavaScript? I don't see any server-side verification of the values. Quote Link to comment https://forums.phpfreaks.com/topic/252607-how-to-explode-on-the-fly/#findComment-1295045 Share on other sites More sharing options...
dflow Posted December 7, 2011 Author Share Posted December 7, 2011 Why not just do it all in JavaScript? I don't see any server-side verification of the values. not very well informed with js to play around ... Quote Link to comment https://forums.phpfreaks.com/topic/252607-how-to-explode-on-the-fly/#findComment-1295226 Share on other sites More sharing options...
dflow Posted December 7, 2011 Author Share Posted December 7, 2011 Hey, I was just taking a look at this and it seems like that should be working. If you're having any trouble it's could be because of the white space you're passing as a part of the deliminator $p = explode(" , ", $_GET['searchField'], 3); Try it like this and see if it helps: $p = explode(",", $_GET['searchField'], 3); and if you want to ensure that you aren't getting any additional white space in your strings you could put that in your variables like echo $cityname=trim($p['0']); echo $regionname=trim($p['1']); echo $countryname=trim($p['2']); Maybe not the most elegant way, but it could be a start. I'm not really sure where you want to go from there, but I'm assuming you might want to perform a search query or store the data in some way if you're using ajax with that as well. I'm not well-versed enough in ajax, but I know jquery has a nice ajax funtion that maybe could make life a little easier. It did for me anyway. -Frank do you have an example with the jquery? Quote Link to comment https://forums.phpfreaks.com/topic/252607-how-to-explode-on-the-fly/#findComment-1295227 Share on other sites More sharing options...
WTFranklin Posted December 8, 2011 Share Posted December 8, 2011 Hey, Xyph has a point that you could do this all in javascript either with a mixture of the indexOf or subString functions to find the comma and store the value into an array if you weren't planning on doing anything to the values server side. If you were moving towards anything with the database though the ajax function works kind of like this: $.ajax({ url: '<link to php file with code you want executed>', type: 'POST' or 'GET', data: {<var_name>: <data>}, dataType: <json, xml, other formats listed in the documentation>, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Error: " + errorThrown + ", " + textStatus); }, success: function(data) { //code to execute if successful //for your code something like $('#place').val = data['city']; etc. } }); Here's a full list of what objects you can pass to $.ajax() http://api.jquery.com/jQuery.ajax/ I hope that helps! -Frank Quote Link to comment https://forums.phpfreaks.com/topic/252607-how-to-explode-on-the-fly/#findComment-1295803 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.