Advice on converting asp to php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinod
    Contributor
    • Nov 2007
    • 347

    Advice on converting asp to php

    I am trying to convert my site to php from classic asp by teaching myself. i use session variables for a shopping cart type of effect on the site and the following asp code setermines the session variable and what to add to it.

    Code:
    <%
    if InStr(session("recordsInCart"), ","&request.form("recordNum")) = 0 then 
       session("recordsInCart") = session("recordsInCart") + request.form("recordNum") &","
    else 
       'do nothing
    end if 
    %>
    can anyone advise on how to change this to php

    thanks

    colin
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    In PHP, to use a session variable you must first call the session_start function. This must be done before ANY output is generated; before any HTML in the page, and before any print or echo statements in PHP.

    Then you can use $_SESSION["name"] to access a session variable named "name".

    To access form data, you either use $_GET or $_POST, depending on the protocol used. For instance, in a POST form, accessing a "recordNum" field would be done like: $_POST["recordNum"].

    To find out if a variable, or array element, is set, you can use the isset function. To find out if a variable or array element is set and has a non-empty value, use the empty function.
    Code:
    if (isset($_POST["number"])) {
        // It is set to something.
    }
    
    if (!empty($_POST["number"])) {
        // It is set AND it is not empty.
    }
    
    if (!empty($_POST["number]) && is_numeric($_POST["number)) {
        // It is set AND it has a numeric value.
    }

    Comment

    Working...