Jump to content

how to explode on the fly?


dflow

Recommended Posts

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.