Sign in to follow this  

[web] php and html form problem?

This topic is 4585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am a total newbie with php, I am still learning the basics. But I have some php and html and I have a form on my page but my form's button won't call my javascript function i have set up. This is weird cause I have basically the same code in another page and it works fine execpt that I am not using variable being passed in through the url. well anyways here is my code for my page, but no means is it finished or even neat but I would just like to know why my form doesnt call my javascript function and if you have any advice yo ucan post it here too. Thanks Jay
<?php
/*
    Aurthor: Japhett Burt
    GhostGamers WebSite
    projects.php - Created on 5/17/05
    This file is the projects page.
*/

require("config.php");
require("common.php");

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>

<HEAD>

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">

<TITLE><?php echo $page_title; ?></TITLE>

<style type="text/css" media="screen">
<!--
	@import url("layout.css");
-->
</style>

<script language="javascript">
  function DoSubmit ()
  {
    if (document.form.mail_from.username == "") {
      alert ("You forgot to enter the 'User Name' field.");
      document.form.username.focus ();
      return "";
    }

    if (document.form.pass.value == "") {
      alert ("You forgot to enter the 'Password' field.");
      document.form.pass.focus ();
      return "";
    }

    document.form.submit ();
  }
</script>

</HEAD>

<BODY>

<div id="wrapper" align="center">

    <div id="header" align="center">
        <?php include("header.php"); ?>
    </div>

    <div id="menu" align="center">
        <?php include("menu.php"); ?>
    </div>

    <div id="content" align="center">

    <div id="projects_content" align="left">
    
        <?php

            $page = $_SERVER['HTTP_REFERER'];
            $page = getfilename($page);
            if($action == 'add' && $page == "projects.php?action=login")
            {
                echo"<br><br><table>
                    <tr>
                      <td>Project Name:</td>
                      <td><input type=\"text\" name=\"project_name\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td>URL of Project Image:</td>
                      <td><input type=\"text\" name=\"project_image\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td>URL of Project Forum:</td>
                      <td><input type=\"text\" name=\"project_forum\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td>URL of Download:</td>
                      <td><input type=\"text\" name=\"project_download\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td>URL of Project Screenshots:</td>
                      <td><input type=\"text\" name=\"project_screenshots\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td>Team Size:</td>
                      <td><input type=\"text\" name=\"project_teamsize\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td>Dev Time:</td>
                      <td><input type=\"text\" name=\"project_devtime\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td>Project Type:</td>
                      <td><input type=\"text\" name=\"project_type\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td>Project Sub-Type:</td>
                      <td><input type=\"text\" name=\"project_subtype\" size=\"40\"></td>
                    </tr>
                    <tr>
                      <td valign=\"top\">Project Description:</td>
                      <td><textarea name=\"project_description\" cols=\"40\" rows=\"10\"></textarea></td>
                    </tr>
                    <tr>
                      <td><input type=\"hidden\" name=\"mode\" value=\"\"></td>
                      <td><input type=\"button\" onclick=\"\" value=\"Add Project\"></td>
                    </tr>
              </table>";
            }
            else if($action == 'view')
            {
            ?>
                <div id=\"add_link\" align=\"right\">
                <a href=\"projects.php?action=login\">- Add Project -</a>
                </div>
            <?php
            }
            else if($action == 'login')
            {

            ?>
                <br><br>
                <form action="projects.php" method="post" name="form">
                <table>
                    <tr>
                      <td>User Name:</td>
                      <td><input type="text" name="username" size="40"></td>
                    </tr>
                    <tr>
                      <td>Password:</td>
                      <td><input type="text" name="pass" size="40"></td>
                    </tr>
                    <tr>
                      <td><input type="hidden" name="mode" value="submit"></td>
                      <td><input type="button" onclick="DoSubmit ()" value="Login"></td>
                    </tr>
              </table>
              </form>
              
            <?php
            }
            else
            {
              echo "<div id=\"add_link\" align=\"right\">
                <a href=\"projects.php?action=login\">- Add Project -</a>
                </div>";
                
              ?>
              
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="form">
                  <table>
                    <tr>
                      <td>From:</td>
                      <td><input type="text" name="mail_from" size="40"></td>
                    </tr>
                    <tr>
                      <td>Subject:</td>
                      <td><input type="text" name="mail_subject" size="40"></td>
                    </tr>
                    <tr>
                      <td valign="top">Body:</td>
                      <td><textarea name="mail_body" cols="40" rows="10"></textarea></td>
                    </tr>
                    <tr>
                      <td><input type="hidden" name="mode" value="submit"></td>
                      <td><input type="button" onclick="DoSubmit ()" value="Send E-Mail"></td>
                    </tr>
                  </table>
                </form>
        <?php
            }
            
        ?>
        
    </div>

    </div>

    <div id="footer" align="center">
        <?php include("footer.php"); ?>
    </div>

</div>

</BODY>

</HTML>



Share this post


Link to post
Share on other sites
Try changing the type from 'button' to 'submit'.

Edit: Button looks like it should work fine, I'll have to look more carefully later, unless someone else answers before then.

Share this post


Link to post
Share on other sites
Try putting the <form> tags outside of the <table> They usually don't like being inside of there unless you keep them together inside 1 cell. Let me know if that helps.

Share this post


Link to post
Share on other sites
I would suggest the following:

1: Change the button for submitting to type="submit" and REMOVE the OnClick attribute for the form.

2: Add the following to the <form> tag:


OnSubmit="return DoSubmit(this)"


3: Edit the DoSubmit() function to look like this:


function DoSubmit(form)
{
if(form.mail_from.username.value == "")
{
alert ("You forgot to enter the 'User Name' field.");
form.username.focus();
return false;
}

if(form.pass.value == "")
{
alert ("You forgot to enter the 'Password' field.");
form.pass.focus();
return false;
}
return true;
}



As a side note, one thing you can do is have a folder called "forms" that has just the HTML code starting from form and going to /form in a separate PHP file that then gets included based on the querystring like you are doing now for the if $action section. Ping me on IM if you want and I can point you at some good resources for that. My IM info is in my profile.

Share this post


Link to post
Share on other sites
1) The first form ('add') has no form tags.
2) document.form.mail_from.username doesn't exists. Use document.form.mail_from or document.form.username
3) Validating both mail_from and pass in the same routine doesn't work, because only one form is showed and it will never contain both these input controls.

Share this post


Link to post
Share on other sites

This topic is 4585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this