Question

I realize there are similar threads regarding .asp pages, however I am using an html form with php involved with the submit action leading into a survey.

The client I am developing for would like either one of the text fields to be filled out, while neither one is hard required one of the two must be filled out. It can be either of the two, however both cannot be left blank.

I would prefer to not use jquery, however if this is necessary I will learn how. I am brand new to all of this and just creating the form and connecting it to the database took a whole lot of learning lol.

Thank you for any help, below I have posted the HTML form, and the php script it is linked to.

<form action="insert.php" method="post">
Name: <input type="varchar" name="name" />
<span style="color: #F00">or</span> Email:
<input type="varchar" name="email" />
<input type="submit" />
</form>

The PHP 'insert.php

<?php
$con = mysql_connect("localhost","gobagtoo_1","1");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("gobagtoo_1", $con);

$sql="INSERT INTO name_email (name, email)
VALUES
('$_POST[name]','$_POST[email]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else 
$url = 'tool.html';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';  

mysql_close($con)
?>

M

Was it helpful?

Solution

You should accomplish this both on the client side (where you see the form) with javascript and on the server side (the PHP after you submit the form).

On the client side: 1. Add the function to the page, validateForm. 2. Add id attributes to your name and email elements. 3. Add an onsubmit element to your form tag.

    <html>
    <head>
    <script language="javascript">
    function validateForm(){
        var value1 = document.getElementById('nameId').value;
        var value2 = document.getElementById('emailId').value;
        if( value1 != "" || value2 != "" ) 
        {
            return true;    
        }
        alert("You must enter a value");
        return false;
    }
    </script>
    </head>

    <body>
    <form action="insert.php" method="post" onsubmit="return validateForm();">
    Name: <input type="text" name="name" id="nameId" /> 
    <span style="color: #F00">or</span> Email: <input type="text" name="email" id="emailId" />
    <input type="submit" /> 
    </form> 
    </body>
    </html>

On the PHP side:

    <?PHP

    function makeSafeish( $value ){
        return mysql_escape_real_string( $value );
        // If you put this in a function, you can do more w/ it. I don't use this as is but don't want to be judged by the community here for my bad use :P
    }

    if( strlen( $_POST['name'] ) > 0 && strlen( $_POST['email'] ) > 0 ){
        $con = mysql_connect("localhost","******","******"); 
        if (!$con) { 
            die('Could not connect: ' . mysql_error()); 
        }  
        mysql_select_db("*****", $con); 
        $sql="INSERT INTO name_email (name, email) VALUES ('".makeSafeish( $_POST["name"] )."','".makeSafeish( $_POST["email"] )."')"; 
        if ( ! mysql_query( $sql, $con ) ) { 
            die('Error: ' . mysql_error()); 
        } else  {
            $url = 'tool.html'; 
        }

        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';    
        mysql_close($con); // you're not gonna get here because of the above line.
    }else{
        echo 'You must enter either an email or name.";
    }
    ?> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top