...

View Full Version : break url up into an array.



this_end_up
08-14-2007, 11:37 PM
Hello,

Currently, I use the following code to call a flat text file to a php page. The flat file is a tab-deliminated file that was generated by a CMS.

<?php
$page = basename($_SERVER['QUERY_STRING']);
if(!$page){
include('contentstandard.php');
} else {
if(file_exists('pages/'.$page.'.txt')){
include('contentgen.php');
} else {
include('nocontent.php');
}
}
?>

When I call the php file with the associated text file, the URL looks like this:
www.????.com/whatever.php?flatfile

On this page (whatever.php) there is a form. I would like to do a prepopulated URL and use a php script to break apart the string into an array so I can prepop the form....the URL I want to use is similar to:
www.????.com/whatever.php?flatfile&name=mike&last=jones&company=AOL

Can anyone help me?

whizard
08-15-2007, 12:56 AM
Which values are you trying to isolate?

If you just want to get the variables, why not use the $_GET superglobal?

for instance, with your example URL, $_GET['name'] would contain 'mike', and $_GET['last'] would hold 'jones'.

HTH
Dan

this_end_up
08-15-2007, 02:37 AM
Dan,

I am trying to isolate the values so that I can place them into the "value" part of a form.

I originally did this with the landing page being an asp file (and using this:<%=Request.Querystring("first")%> within the "value" of my input field. therefore, if I have the persons info, then I can just add it to the URL string so that they will not have to fill out the form...I already filled it for them.

I would like to something similar, but it seems that the only way to do this is to break apart the string into an array. I still want the first section of the URL (after the"?") to be the caller of the flat text file.


Anything you can do to help...

Inigoesdr
08-15-2007, 02:51 AM
The equivalent would be
<?=$_GET['first'];?>

whizard
08-15-2007, 05:35 PM
Inigoesdr is correct.

in PHP, all the querystring values are already stored in an array, the $_GET superglobal array.

to access them, you use

$_GET['variablename'] which would return 'variablevalue'.

HTH
Dan

this_end_up
08-15-2007, 07:00 PM
That worked great. I had a file that was used to strip the flat file apart that I didn't apply this to, so it wasn't working. Now it is working, and it works great...but I have another issue.

I have placed a script that is called "cookie saver" which basically creates a cookie and saves to the cookie the form fields, so that if somebody returns to the php landing page to request another document, the form will already be filled out. It is a great little script.

The issue that I have is:

So now if I use:
www.???????.com/whatever.php?id=flatfile&first=michael

the cookie saver overwrites the <?=$_GET['first'];?>, with the cookie. And even if it is a page that has never been visited, the <?=$_GET['first'];?> shows up for a brief second, then dissapears. Any suggestions?

Below is the cookie saver code, followed by my input field.


Cookie saver code:
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// Cookie Functions //////////////////// (:)
// Set the cookie.
// SetCookie('your_cookie_name', 'your_cookie_value', exp);
// Get the cookie.
// var someVariable = GetCookie('your_cookie_name');

var expDays = 100;

var exp = new Date();

exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {

var endstr = document.cookie.indexOf (";", offset);

if (endstr == -1) { endstr = document.cookie.length; }

return unescape(document.cookie.substring(offset, endstr));

}

function GetCookie (name) {

var arg = name + "=";

var alen = arg.length;

var clen = document.cookie.length;

var i = 0;

while (i < clen) {

var j = i + alen;

if (document.cookie.substring(i, j) == arg) return getCookieVal (j);

i = document.cookie.indexOf(" ", i) + 1;

if (i == 0) break;

}

return null;

}

function SetCookie (name, value) {

var argv = SetCookie.arguments;

var argc = SetCookie.arguments.length;

var expires = (argc > 2) ? argv[2] : null;

var path = (argc > 3) ? argv[3] : null;

var domain = (argc > 4) ? argv[4] : null;

var secure = (argc > 5) ? argv[5] : false;

document.cookie = name + "=" + escape (value) +

((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +

((path == null) ? "" : ("; path=" + path)) +

((domain == null) ? "" : ("; domain=" + domain)) +

((secure == true) ? "; secure" : "");

}

// cookieForms saves form content of a page.

// use the following code to call it:

// <body onLoad="cookieForms('open', 'form_1', 'form_2', 'form_n')" onUnLoad="cookieForms('save', 'form_1', 'form_2', 'form_n')">

// It works on text fields and dropdowns in IE 5+

// It only works on text fields in Netscape 4.5

function cookieForms() {

var mode = cookieForms.arguments[0];



for(f=1; f<cookieForms.arguments.length; f++) {

formName = cookieForms.arguments[f];



if(mode == 'open') {

cookieValue = GetCookie('saved_'+formName);

if(cookieValue != null) {

var cookieArray = cookieValue.split('#cf#');



if(cookieArray.length == document[formName].elements.length) {

for(i=0; i<document[formName].elements.length; i++) {



if(cookieArray[i].substring(0,6) == 'select') { document[formName].elements[i].options.selectedIndex = cookieArray[i].substring(7, cookieArray[i].length-1); }

else if((cookieArray[i] == 'cbtrue') || (cookieArray[i] == 'rbtrue')) { document[formName].elements[i].checked = true; }

else if((cookieArray[i] == 'cbfalse') || (cookieArray[i] == 'rbfalse')) { document[formName].elements[i].checked = false; }

else { document[formName].elements[i].value = (cookieArray[i]) ? cookieArray[i] : ''; }

}

}

}

}



if(mode == 'save') {

cookieValue = '';

for(i=0; i<document[formName].elements.length; i++) {

fieldType = document[formName].elements[i].type;



if(fieldType == 'password') { passValue = ''; }

else if(fieldType == 'checkbox') { passValue = 'cb'+document[formName].elements[i].checked; }

else if(fieldType == 'radio') { passValue = 'rb'+document[formName].elements[i].checked; }

else if(fieldType == 'select-one') { passValue = 'select'+document[formName].elements[i].options.selectedIndex; }

else { passValue = document[formName].elements[i].value; }



cookieValue = cookieValue + passValue + '#cf#';

}

cookieValue = cookieValue.substring(0, cookieValue.length-4); // Remove last delimiter



SetCookie('saved_'+formName, cookieValue, exp);

}

}

}

// End -->
</script>


Form input field script:
<input maxlength="40" type="text" id="first_name" size="20" name="first_name" class="req" value="<?=$_GET['first'];?>" />

meth
08-15-2007, 09:20 PM
PHP processes the html for this page as it leaves the server. During this time, php populates the input field, as it's told to, with "<?=$_GET['first'];?>".

When the page loads in the user's browser, the javascript executes and removes the value if required.

So if you don't want that initial display of the value in the input field, delete "<?=$_GET['first'];?>" from the value attribute.

Inigoesdr
08-16-2007, 12:27 AM
PHP processes the html for this page as it leaves the server. During this time, php populates the input field, as it's told to, with "<?=$_GET['first'];?>".

When the page loads in the user's browser, the javascript executes and removes the value if required.

So if you don't want that initial display of the value in the input field, delete "<?=$_GET['first'];?>" from the value attribute.
Or, he could fix the JavaScript that's overwriting it. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum