PHP 表单需要两个文本字段之一

发布于 2025-01-07 06:38:49 字数 1012 浏览 1 评论 0原文

我意识到关于 .asp 页面有类似的线程,但是我使用的是 html 表单,其中 php 涉及导致调查的提交操作。

我正在开发的客户希望填写其中一个文本字段,但这两个字段都不是硬性要求,必须填写其中之一。可以是两者中的任何一个,但是两者都不能留空。

我不想使用jquery,但是如果有必要我会学习如何使用。我对这一切都是全新的,只是创建表单并将其连接到数据库就需要大量的学习,哈哈。

感谢您的帮助,下面我发布了 HTML 表单及其链接到的 php 脚本。

<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>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在夏天 2025-01-14 06:38:49

您应该使用 JavaScript 在客户端(您看到表单的地方)和服务器端(提交表单后的 PHP)完成此操作。

在客户端:
1. 页面添加函数validateForm。
2. 将 id 属性添加到您的姓名和电子邮件元素。
3. 将 onsubmit 元素添加到表单标记中。

    <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>

在 PHP 方面:

    <?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.";
    }
    ?> 

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.";
    }
    ?> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文