SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Web page initialization

    I can't find anything on how a web page initializes. In other words, before the page comes up, I want to put default values in some fields. I've tried the
    if($_SERVER['REQUEST_METHOD'] == 'GET') to no avail. Meaning, it doesn't even go into the if statement.

    Help please.

    Thank you!

  2. #2
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By 'put default values in some fields' do you mean you want to put some data into an actual form on your page? Please explain further what you need.
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  3. #3
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Put a name on the input button of your form

    Code:
    <input type="submit" name="submit_button">
    Then on the top put something like

    Code:
    if (!isset($_REQUEST['submit_button']))
    LIST OF DEFAULT VALUES
    In your form put the default value in the value

    Code:
    <input type="text" value="<?= $default ?>" />

  4. #4
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I'm getting data from a MySQL data base and I've been doing pretty much what galen suggested except the part about the '$REQUEST'.
    So, when the page is first brought up, it reads a cookie, takes that value populates a form (which isn't happening completely right now). I thought I could have an 'if' statement and and $_SERVER['REQUEST_METHOD'] == 'GET', that code would execute only when the page is loaded. My form is of method='post'.
    Anyway, I worked around it by $_SERVER['REQUEST_METHOD'] !='POST' which does what I want now - I think.
    <-------- Part of the Listings: ------------->
    (this code included by following file....)
    if($_SERVER['REQUEST_METHOD']!='POST'){

    //$articleID = $_POST['articleID']; //Needs to be seesion value
    $articleID = $articleCookie;
    $sql = "SELECT htmlLink, summary, publish, datetime FROM newsFromWeb where articleSerialNum = '$articleCookie'";

    // Arrray of all the variables that came back.
    $results = mysql_query($sql, $conn) or die('Error, Sql query failed on SELECT');

    if(!$results){
    $message = "Invalid query: ";
    $message .= mysql_error();
    $message .= "\n";
    $message .= "Whole query: " .$sql;
    die($message);
    }

    $row = mysql_fetch_assoc($result);

    $htmlLink = $row["htmlLink"];
    $summary = $row["summary"];
    $publish = $row["publish"];
    $timeStamp = $row["datetime"];

    if(mysql_num_rows($results) != 0 ){
    $feedback = "Select for " .$htmlLink ." processed correctly. for articleID: " .$articleID ."num rows " .mysql_num_rows($results) ;
    }
    else{
    $feedback = "mysql_query failed.";
    }
    mysql_free_result($results);
    }

    <----- And from the code that has the form and includes above : ----- >
    <form name="edit_form" id="edit_form" method="post" action="articleEditor.php" class="c7" onsubmit="RefreshAction(this)">
    ....
    <td colspan="6" valign="top"><input name="articleLink" type="text" value="<?php echo __($htmlLink); ?>" class="c9"></td>
    <td colspan="8"/>
    </tr>
    <tr>
    <td height="12"/>
    <td colspan="14"/>
    </tr>
    <tr>
    <td height="24"/>
    <td width="126" colspan="3" class="c8">Summary</td>
    <td colspan="11"/>
    </tr>
    <tr>
    <td height="107"/>
    <td colspan="9" valign="top"><textarea name="summary" class="c10"><?php echo __($summary); ?></textarea></td>


    <-------end code listings----->


    Anyway, the form's SQL code is executing and retrieving rows, but for some reason, those 'php' statements that have the variables are not showing up and I'm wondering if it's because I have my initialization timing off. Or something so that the variables are not being filled by the time the page is sent to the browser.
    (Sorry about my terminology, I'm working on it but I've come from a Windows and OS/2 executable development background.)
    Thanks everyone!!

  5. #5
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, yeah, the above code isn't everything because I didn't want have this gigantic post for you folks to read.
    Last edited by JustaLinuxGuy; Oct 29, 2007 at 06:49. Reason: clariy

  6. #6
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,345
    Mentioned
    192 Post(s)
    Tagged
    5 Thread(s)
    I suggest you verify that the variables actually have values.
    Use (my favorite) "var_dump" command, in your HTML, to display the contents of $summary, $htmlLink, et cetera.
    Like this:
    <?php var_dump($summary, $htmlLink, $publish, $timeStamp); ?>
    This confirms the SQL results are correct and that the variables are being populated correctly. Otherwise, it appears you are applying the values as you described.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's why you use PHP BBCODE tags. For example:

    PHP Code:
    <-------- Part of the Listings: ------------->
    (this code included by following file....)
    if($_SERVER['REQUEST_METHOD']!='POST'){

    //$articleID = $_POST['articleID']; //Needs to be seesion value
    $articleID = $articleCookie;
    $sql = "SELECT htmlLink, summary, publish, datetime FROM newsFromWeb where articleSerialNum = '$articleCookie'"; 

    // Arrray of all the variables that came back.
    $results = mysql_query($sql, $conn) or die('Error, Sql query failed on SELECT');

    if(!$results){
    $message = "Invalid query: ";
    $message .= mysql_error();
    $message .= "\n";
    $message .= "Whole query: " .$sql;
    die($message);
    }

    $row = mysql_fetch_assoc($result);

    $htmlLink = $row["htmlLink"];
    $summary = $row["summary"];
    $publish = $row["publish"];
    $timeStamp = $row["datetime"];

    if(mysql_num_rows($results) != 0 ){
    $feedback = "Select for " .$htmlLink ." processed correctly. for articleID: " .$articleID ."num rows " .mysql_num_rows($results) ;
    }
    else{
    $feedback = "mysql_query failed."; 
    }
    mysql_free_result($results);
    }

    <----- And from the code that has the form and includes above : ----- >
    <form name="edit_form" id="edit_form" method="post" action="articleEditor.php" class="c7" onsubmit="RefreshAction(this)">
    ....
    <td colspan="6" valign="top"><input name="articleLink" type="text" value="<?php echo __($htmlLink); ?>" class="c9"></td>
    <td colspan="8"/>
    </tr>
    <tr>
    <td height="12"/>
    <td colspan="14"/>
    </tr>
    <tr>
    <td height="24"/>
    <td width="126" colspan="3" class="c8">Summary</td>
    <td colspan="11"/>
    </tr>
    <tr>
    <td height="107"/>
    <td colspan="9" valign="top"><textarea name="summary" class="c10"><?php echo __($summary); ?></textarea></td>


    <-------end code listings----->
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, more news.....<p/>Even though MySQL 'SELECT' is coming back "successful", the value from my $row['htmlLink'], for example, isn't getting into my local variable, or nothing is really coming back. I'll use the var_dump command to see what's going on. <p/>Thanks for checking to see if I'm doing something really dump.<p/>'BBCODE' - learn something new everyday! , Thanks!

  9. #9
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     if(!$results){
    $message = "Invalid query: ";
    $message .= mysql_error();
    $message .= "\n";
    $message .= "Whole query: " .$sql;
    die($message);
    }
    ^ That is useless because you already include a die after the query


    also register_globals is probably off so

    Code:
     $articleID = $articleCookie;
    would do nothing and teh sql statement wouldn't return anything. Meaning your $row['htmlLink'] woudl be empty.

    Try

    Code:
     $articleID = $_COOKIE['articleCookie'];

  10. #10
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After doing a "var_dump":
    var_dump($sql);
    string 'SELECT htmlLink, summary, publish, datetime FROM newsFromWeb where articleSerialNum = '75'' (length=90)
    -- SQL OK.

    var_dump($results);
    resource(4, mysql result)
    -- Should return 4 - OK

    $row = mysql_fetch_assoc($result);
    var_dump ($row)
    boolean false
    --false?!?!

  11. #11
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's actually two single quotes after the '75'.

  12. #12
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Complete stupidity on my part!!!!


    I used teo different variable names: result and results. Doh!!!!!!!
    I've been staring at this code for too long!!

  13. #13
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still, thanks for the input! I've learned a lot about my handling of MySQL errors and var_dump, etc....

    Thanks!!


Bookmarks

Posting Permissions

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