Jump to content

HTML Forms and PHP


yO2gO

Recommended Posts

Hi,

 

I am very new to PHP and am having trouble understanding how HTML, PHP and JavaScript work together.

 

What I have to do is, I have a 'Choose File' option, where I select the file. Then I transfer this file to server and read the contents in an array. This is done in one form.

 

In the other form I send the contents read from the to a different *.php for some database querying.

 

Following is what I have. I don't know how to transfer data between different files.

 

<form  name ="INPUT" method="POST" enctype="multipart/form-data" action="readInput.php">
                            <p id="upload_text"><br/>
                                <textarea name="queryList" id="queryList" cols="35" rows="10" wrap="physical" value='' onclick ="document.INPUT.queryList.value='';">
                                    Enter input here...
                                </textarea><br/>
                            </p>
                            <p>
                                <!-- <input name=INPUTS type=hidden value=$entries[]/>-->
                            </p>
                            <p id="upload_button"><br/>
                                File:
                                <input  name="uploadedfile" type="file" size="30" onchange='fillTextArea(<?php echo $entries; ?>)'/><br/>
                            </p>
                            <iframe id="upload_target" name="upload_target" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
                            <a href="javascript: submitReadForm()"></a>
                        </form>

                        <script type="text/javascript">
                            function submitReadForm() {
                                document.INPUT.submit();
                            }
                        </script>

                        <script language="javascript" type="text/javascript">
                            //var inData = new Array();
                            function fillTextArea(inData){
                                if (inData == "") {
                                    document.getElementById('queryList').textContent = "No Input"
                                    alert("No Input Data Returned");
                                } else {
                                    document.getElementById('queryList').textContent = inData;
                                }
                            }

                        </script>


                        <form  name ="GETPDB" action ="viewPDBs.php" method="POST" enctype="multipart/form-data"  >
                            <p id="submit_upload">
                                <input type="submit" name="submit" value="Submit" /><br/>
                            </p>
                            <p>
                                <!-- #<#?php echo $entries; ?>
                                #<#php foreach ($entries as $k => $e) : ?>
                                    <input type="hidden" name="entries[<#?php echo $k ?>]" value="<#?php echo addslashes($e) ?>">
                                    #<#?php endforeach; ?> -->

                                <input name=IDS type=hidden value="<?php echo $input; ?>"/>
                            </p>
                            <a href="javascript: submitPDBForm()"></a>
                        </form>

 

Please help me with this. Thanks.

Link to comment
Share on other sites

well i should first of all, try to get a form working without any javascript.

javascript can be disabled by the enduser (clientside) so it should only be used for extra awesomeness

That said,

hereunder you find a nice (self referencing) form with limited validation that shows the post data on the next page load. you can use another file, just fill in the file name at the form action.

If you like to use submitted variables on other pages than the form action is pointing at, have a read about sessions.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-language" content="en" />
        <title>my form</title>
</head>
    <body>
        <?php
        $error_messages = array();

        if(isset($_POST['submit'])){ //check if pressed submit
            if(!empty($_POST['firstname'])&&!empty($_POST['lastname'])&&!empty($_POST['age'])){//no field may be empty
                echo 'you succesfully submited post data';
            }else{//if pressed submit but fields are empty
                $error_messages[]= 'you left fields empty';
            }
        }else{// if not not pressed submit
            $error_messages[]= 'please fill in the form';
        }

        ?>
        <form id="myform" action="" method="post">
            <ul style="text-align: right; float:left; list-style: none;">
                <li><label for="firstname">firstname:</label><input id="firstname" type="text" name="firstname" value="<?php echo isset($_POST['firstname'])? $_POST['firstname']: '' ;?>" /></li>
                <li><label for="lastname">lastname:</label><input id="lastname" type="text" name="lastname" value="<?php echo isset($_POST['lastname'])? $_POST['lastname']: '' ;?>" /></li>
                <li><label for="age">age:</label><input id="age" type="text" name="age" value="<?php echo isset($_POST['age'])? $_POST['age']: '' ;?>" /></li>
                <li><input type="submit" value="submit" name="submit" /></li>
            </ul>
        </form>
        <p id="errormessages"><?php
            if (!empty($error_messages)){ //if there are errors
                echo implode('<br />', $error_messages); //output error messages array
            }
        ?></p>
    </body>
</html>

 

hope this helps

 

p.s. in real life i would have not used inline css

 

 

-edit: i made a little change i had the name of the submit button set to 'enter'  I changed it to submit otherwise nothing would work ::)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.