Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8700561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:11:40+00:00 2026-06-13T02:11:40+00:00

This question will make you think :D. I have a HTML input form with

  • 0

This question will make you think :D. I have a HTML input form with as it seems too many functions. One function is there so when enter is pressed it submits data. Other function is there to check if certain keyword is entered so it can redirect people to other URL. It all works perfectly thx to people from stack overflov. Here is my code:

<script type="text/javascript"> 
<!-- 
function enter(e){ 
    if(e.keyCode == 13) 
    { 
        Login(); 
        return false; 
    } 
} 
//--> 
</script> 


    <script type="text/JavaScript"> 
     <!-- 
     function Login(){ 
     var keyword=document.getElementById("address").value; 
     var done=0; 
     keyword=keyword.toLowerCase(); 
     keyword=keyword.split(' ').join(''); 
     if (keyword=="example,example") { window.location="http://www.example.com"; done=1;} 
     if (keyword=="example1") { window.location="http://www.example1.com"; done=1;} 
     if (keyword=="example2") { window.location="http://www.example2"; done=1;} 
     if (done==0) { doSomethingElse(); } 
     } 
     //--> 
     </script>  

    <form name="enterkeyword" action="none"> 
        <input name="keyword" id="address" type="text" onkeypress="return enter(event);"/> 
        <div class="buttons"> 
        <button type="button" onclick="Login()">Submit</button> 
        </div> 
        </form> 

Here is where the problem is. I need to find a way to save every submitted query to excel or .CSV. I found a way that saves input data to .CSV file using PHP. I tested it and it works. BUT when i try to implement that with existing functions on my input form it messes up everything. Here is the code from “query saving function”, this is HTML part:

<html>
<head>
    <title>My Form</title>
</head>

<body>
    <form action="save.php" method="post">
        <p>
            What is your favorite movie?<br>
            <input type="text" name="formMovie" maxlength="50" value="<?=$varMovie;?>" />
        </p>
        <p>
            What is your name?<br>
            <input type="text" name="formName" maxlength="50" value="<?=$varName;?>" />
        </p>                
        <input type="submit" name="formSubmit" value="Submit" />
    </form>
</body>
</html>

This is PHP part (save.php):

<?php
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage = "";

    if(empty($_POST['formMovie']))
    {
        $errorMessage .= "<li>You forgot to enter a movie!</li>";
    }
    if(empty($_POST['formName']))
    {
        $errorMessage .= "<li>You forgot to enter a name!</li>";
    }

    $varMovie = $_POST['formMovie'];
    $varName = $_POST['formName'];

    if(empty($errorMessage)) 
    {
        $fs = fopen("mydata.csv","a");
        fwrite($fs,$varName . ", " . $varMovie . "\n");
        fclose($fs);

        exit;
    }
}
?>

    <?php
        if(!empty($errorMessage)) 
        {
            echo("<p>There was an error with your form:</p>\n");
            echo("<ul>" . $errorMessage . "</ul>\n");
        } 
    ?>

Both codes work great but i just dont know how to implement them so they can work togeather for same form… BTW this PHP function has 2 input fields, i need only 1, and i really dont need this error message if fields are left empty… I know its complicated but can someone who understands this better than me help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:11:41+00:00Added an answer on June 13, 2026 at 2:11 am

    Complete update

    Hey again, I seperated the functionalities and made the code readable and maintainable.

    Index.html

    <!DOCTYPE html>
    <html>
        <head>
            <link rel="stylesheet" href="styles/index.css"/>
        </head>
        <body>
            <input type="text" id="txt" value="example1"/><br/>
            <input type="button" id="btn" value="doQuery()"/>
    
            <script src="scripts/index.js"></script>
        </body>
    </html>
    

    Index.js

    function processValue() {
    
        var inputText = document.getElementById("txt").value.toLowerCase().split(' ').join('');
    
        // Export query
        exportQuery( inputText );
    }
    
    function exportQuery( query ) {
    
        var script = document.createElement("SCRIPT");
    
        script.setAttribute( 'type', 'text/javascript' );
        script.setAttribute( 'src', 'exportQuery.php?query=' + query );
    
        document.head.appendChild( script );
    }
    
    var urlMapper = {
    
        "example,example": "http://www.example.com",
        "example1": "http://www.example1.com",
        "example2": "http://www.example2.com"
    }
    
    /**
     * Should be called when the PHP has finished execution
     */
    function onPhpReady( query ) {
    
        var url = urlMapper[ query ];
    
        if( url !== undefined ) {
    
            goToUrl( url );
    
        } else {
    
            codeAddress();
        }
    }
    
    function goToUrl( url ) {
    
        window.location = url;
    }
    
    function codeAddress() {
    
        alert( "Access Denied" );
    }
    
    ( function() {
    
        // Selfexecuting code.
    
        function addButtonClickListener() {
    
            var btn = document.getElementById("btn");
    
            btn.addEventListener( "click", processValue, false );
        }
    
        function processKeyPress( e ) {
    
            if( e.keyCode == 13 ) {
    
                processValue();
            }
        }
    
        function addInputKeyPressListener() {
    
            var input = document.getElementById("txt");
    
            input.addEventListener( "keypress", processKeyPress, false );
        }
    
        addButtonClickListener();
        addInputKeyPressListener();
    
    }() );
    

    exportQuery.php

    <?php
        // Get the request
        $query = $_GET['query'];
    
        if( !empty( $query ) ) 
        {
            writeToCSV( $query );
            notifyReady( $query );
        }
    
        function writeToCSV( $query ) {
    
            $fs = fopen( "storedQueries.csv", "a" );
    
            fwrite( $fs, $query . "\n" );
    
            fclose( $fs );
        }
    
        function notifyReady( $query ) {
    
            // print back as plain javascript
            print <<<ENDLINE
            onPhpReady( '$query' )
    ENDLINE;
            // The ENDLINE ^ can't use spaces or it will give a parse error.
        }
    ?>
    

    Sources: DaniWeb

    Please not that I targeted Webkit, so for compatibility you’ll have to change the way events are attached etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know many have asked this question, but I think my situation is a
Sorry if this question will sound too chaotic, feel free to edit it. I
I know this question will be closed because I studied many example of this
This is confusing me, so this question will probably be confusing. I have a
This question will be a little longer and I am sorry for that :)
This question will be short and sweet. I know an instruction can occur between
( Late edit: This question will hopefully be obsolete when Java 7 comes, because
My guess is that this question will fall under the category of duh, but,
I guess this question will sound familiar, but I am yet another programmer baffled
I hope this question will not be rejected as there is no code. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.