Web Analytics Made Easy -
StatCounter Php help - CodingForum

Announcement

Collapse
No announcement yet.

Php help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Php help

    Hi,

    I'm new to php, but however, I can manage small changes.

    I've a small html/java script. I want to achieve the same through php.
    Please help, how can I do this.

    create a new html file, copy the below script, save the file and run it in a browser. I want to get the same results using a php file. Please help me how to do this. Thanks in advance.

    <script type="text/javascript">
    function addItem(itemValue,outputDestination){
    formElement = document.forms['myForm'].elements[outputDestination]
    tempVar = formElement.value
    if (tempVar.lastIndexOf("\n") != tempVar.length-1){
    }

    formElement.value = itemValue;

    }
    </script>

    <form name="myForm" id="myForm">
    <select onchange="addItem(this.value,'textOutput2')">
    <option value="A1">A1</option>
    <option value="A2">A2</option>
    <option value="A3">A3</option>
    <option value="B1">B1</option>
    <option value="B2">B2</option>
    <option value="B3">B3</option>
    <option value="C1">C1</option>
    <option value="C2">C2</option>
    <option value="C3">C3</option>
    </select>


    <textarea style="displaynone;" name="textOutput2" cols="10" rows="1"></textarea>
    <br />

    </form>


    Regards,
    Vinay

  • #2
    PHP and Javascript are functionally different languages. PHP is run server side, meaning that it pre-processes data, possibly builds html/css/js code, then returns that to the client. Javascript and html are handled on the client side. Needless to say, validation should always occur server side, and client side if desired.

    However, what you're asking for is not possible in the same sense using PHP. PHP requires page refreshes to handle this, and to correctly add the stack you'd need to keep a rather large session array for this as well. This would look something like this:
    PHP Code:
    <?php
    session_start
    ();

    if (!isset(
    $_SESSION['outputOptions']) || !is_array($_SESSION['outputOptions']))
    {
        
    $_SESSION['outputOptions'] = array();
    }

    if (isset(
    $_POST['selOption']))
    {
        if ([email protected]
    get_magic_quotes_gpc())
        {
            
    $_POST['selOption'] = addslashes($_POST['selOption']);
        }
        
    $_SESSION['outputOptions'][] = $_POST['selOption'];
    }

    $aSelectedStack array_map('htmlspecialchars'$_SESSION['outputOptions']);
    $sSelected implode(PHP_EOL$aSelectedStack);

    // Then you echo out the $sSelected into the textarea.  Form not included :)
    As you can see, this is more difficult in PHP, and defintely doesn't have the same effect since this requires page reloads.

    Oh, this is untested btw.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      Ok, if not dynamically, can this be done like this..

      Once the data is selected from a drop down and the form is submitted, can the values in the drop down be copied to a text box (after submitting the form?

      Regards,
      Vinay

      Comment


      • #4
        Yeah, the selection menu will exist in either $_GET or $_POST superglobal depending on you're forms method. This data can be reflected into the text area if you use a self referential form (the action just points back to the same page).

        PHP Code:
        <?php

        $sText 
        '';
        if (isset(
        $_POST['selOption']))
        {
            
        $sText $_POST['selOption'];
        }

        ?>
        <form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
            <fieldset>
                <select name="selOption">
                    <option value="....">...</option>
                </select>
                <!-- Text input -->
                <input type="text" name="txtInput" value="<?php echo $sText;?>" />
                <!-- Text Area -->
                <textarea name="textOutput2" cols="10" rows="1"><?php echo $sText;?></textarea>

            </fieldset>
            <div>
                <input type="submit" value="Submit" />
            </div>
        </form>
        Something along that lines. Don't forget to handle any validation you'll need, including using htmlspecialchars where necessary. This depends only on what the value of the option's are.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

        Comment

        Working...
        X