简体   繁体   English

Javascript,jQuery,PHP问题。

[英]Javascript, jQuery, PHP issue.

<?
session_start();
include("connection.php"); 

if($_POST['continue']) 
{
    $x=$_POST['rules'];
}
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link type="text/css" href="jquery-ui-1.7.2.custom.css" rel="stylesheet" />
    <script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
    <script type="text/javascript" src="jquery-1.3.2.js"></script>
    <script type="text/javascript" src="ui/ui.core.js"></script>
    <script type="text/javascript" src="ui/ui.draggable.js"></script>
    <script type="text/javascript" src="ui/ui.resizable.js"></script>
    <script type="text/javascript" src="ui/ui.dialog.js"></script>
    <script type="text/javascript" src="external/bgiframe/jquery.bgiframe.js"></script>

    <script type="text/javascript">

    function haha(form) 
    {    
        if(form.rules.value=='')
        {
            printToPage('output','Please enter the rules.','text')
            hello();
            return false;               
        }
        else
        {
            myRedirect();
            return false;
        }
    }

    $(function() 
    {
        $("#dialog").dialog({
            autoOpen: false,
            bgiframe: true,
            resizable: false,
            draggable: false,
            height:10,
            width:340,
            modal: true,
            overlay: 
                    {
                       backgroundColor: '#000',
                       opacity: 0.5
                    },
            buttons: 
                    {
                       'No': function() 
                        {
                           window.location = "so-rules.php";
                           return true;
                        },
                       'Yes': function() 
                        {
                           window.location = "so-rules.php";
                           return true;
                        }
            }
        });
    });

    function myRedirect() 
    {
       $("#dialog").dialog('open');
       return true;
    }

    $(function()
    {
       $("#dialog2").dialog
             ({
            autoOpen: false,
            bgiframe: true,
            modal: true,
            resizable: false,
            draggable: false,
            height:160,
            width:260,
            buttons: 
                    {
                        Ok: function() 
                        {
                            $(this).dialog('close');
                        }
                    }
        });
    });

    function hello() 
    {
       $("#dialog2").dialog('open');
    }

    function getElem(id) 
    {
        return document.all ? document.all(id) :
        document.getElementById ? document.getElementById(id) :
        document.layers ? document.layers[id] :
        null;
    }

    function printToPage(id,content,classname) 
    {
       var el = getElem(id);

       if (!el) return;

       if (el.style) 
       {
          el.innerHTML = content;

          if (classname) el.className = classname;
       }
       else if (el.document) 
       {
          var SPANstr = (classname) ? '<span class="' + classname + '">' : '<span>';
          el.document.write('haha');
          el.document.close();
       }
    }
    </script>

</head>

<body>


   <td height="" bgcolor="#fafb91"><form onsubmit='return haha(form)' id="form" name="form" method="post" action="<? echo $PHP_SELF; ?>"> 
          <p class="style16">
          <div align="left">

            <p><span class="style5">Rules:</span>
              </p>

            <p>
              <textarea name="rules" rows="7" cols="49"></textarea>

              <br />
              <? echo "X: ".$_SESSION['x']; ?>
            </p>
            <div id="dialog" title="Attention">
            <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Is the judge/speaker/facilitator from UST?</p>
            </div>

            <div id="dialog2" title="Attention">
            <p>
            <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 0 50px 0;"></span>
            Please enter the rules.
            </p>
            </div>

            <p><input type="submit" name="continue" id="continue" value="Continue"> 
            <span id="output"></span>

</body>
</html>

When the textarea is not null, the form should be submitted ($PHP_SELF) so I can get the value of the textarea. 当textarea不为null时,应提交表单($ PHP_SELF),以便获得textarea的值。 But before it is submitted, a dialog box would appear. 但是在提交之前,将出现一个对话框。 When the user clicks yes, he will be redirected to a different page. 当用户单击“是”时,他将被重定向到另一个页面。 My problem is I don't know where to put the return 'true' so that the page will be submitted in order for me to get the value of the textarea. 我的问题是我不知道将返回值“ true”放在何处,以便提交页面以使我获得textarea的值。 I put the return 'false' here: 我将返回值“ false”放在这里:

if(form.rules.value=='')
{
   printToPage('output','Please enter the rules.','text')
   hello();
   return false;    
}

When the textarea is null, the form will not submit and a different dialog box will appear. 当textarea为null时,将不提交表单,并且将显示另一个对话框。 Where should I put the return 'true' so that the page will only be submitted when the user inputs something in the textarea and after he clicks 'yes'. 我应该在哪里放置返回值“ true”,以便仅在用户在文本区域中输入某些内容并且单击“是”后才提交页面。

I have posted the code where I tried putting in the return 'true' without success. 我将代码粘贴到尝试返回“ true”但未成功的地方。

如果它是type =“ button”,请使用onclick事件来控制操作

Button inputs are used for custom actions that are usually written in JavaScript. 按钮输入用于通常用JavaScript编写的自定义操作。 That is, they do not submit a form like the Submit inputs. 也就是说,他们不会像“提交”输入那样提交表单。 You can write a JavaScript code that will submit the form for you OR navigate to a particular PHP page and achieve a similar effect. 您可以编写JavaScript代码来为您提交表单,也可以导航到特定的PHP页面并达到类似的效果。

If what you are asking is: How can I see in the POST response what button has been clicked? 如果您要问的是:如何在POST响应中看到单击了什么按钮?

Then I'm afraid you can't as only the name and value of the submit button that was used to submit the form is being listed in the $_POST data. 然后,恐怕您不能因为$_POST数据中仅列出了用于提交表单的提交按钮的名称和值。

You can however achieve this using a hidden field and javascript. 但是,您可以使用隐藏字段和javascript实现此目的。 So you should do something similar to this: 因此,您应该执行以下操作:

<input type="button" id="continue" value="Continue" onclick="document.getElementById('hiddenContinue').value = 'true';" />
<input type="hidden" name="continue" id="hiddenContinue" value="false" />

So when the button is clicked the hidden field is set to true and when the form is submitted it appears in the $_POST array. 因此,当单击按钮时,隐藏字段将设置为true,并且在提交表单时,它将显示在$_POST数组中。

However, this doesn't make much sense if there are no other onclick events added to the button. 但是,如果没有其他onclick事件添加到按钮,则这没有多大意义。

Maybe you should describe a little bit more what you are trying to do. 也许您应该多描述一些您想做的事情。

EDIT : 编辑

Since you updated your old question to a new one, this answer doesn't apply anymore. 由于您将旧问题更新为新问题,因此该答案不再适用。

I wonder why you are using "button" and not "submit"? 我想知道为什么您使用“按钮”而不是“提交”? "button" comes in handy when you are using JavaScript, but it doesn't work with PHP. 当您使用JavaScript时,“按钮”会派上用场,但不适用于PHP。 Furthermore, they makes no difference anyway. 此外,它们仍然没有区别。
If all that you want to do is to differentiate between the buttons, try giving them different names. 如果您要做的只是区分按钮,请尝试使用不同的名称。
Hope that help 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM