Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    break url up into an array.

    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:
    http://www.????.com/whatever.php?fla...es&company=AOL

    Can anyone help me?

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    The equivalent would be
    PHP Code:
    <?=$_GET['first'];?>

  • #5
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    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
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #6
    New Coder
    Join Date
    Aug 2007
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    http://www.???????.com/whatever.php?...&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'];?>" />

  • #7
    Regular Coder meth's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Thanks
    0
    Thanked 9 Times in 9 Posts
    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.
    I do Web Design, Brisbane based.
    More time spent in PHP/MySQL Web Development.
    And Search Engine Optimisation takes up the rest of it.

  • #8
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by meth View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •