简体   繁体   English

是否可以显示带有网址的div?

[英]Is it possible to show a div with an url?

I created the sign up form as a hidden div above the whole website with some js. 我使用一些js将注册表单创建为整个网站上方的隐藏div。 Now, I want to create the php part, in a different file. 现在,我想在另一个文件中创建php部分。 I have 3 files. 我有3个档案。 The html, where is the div, form. html格式在哪里,div在哪里。 The js file, where is the switch between the hidden and show state, and it's called by a button. js文件,其中是隐藏状态和显示状态之间的切换,由一个按钮调用。 And the 3. one is the php, where are the if-s(empty fields, wrong datas, etc.), the mysql stuff. 3. 3.一个是php,其中是if-s(空字段,错误数据等),这是mysql的东西。 If the user make a mistake, I want to send back to the main html file, where is the form, with the given datas in the url, so I can say what was the problem. 如果用户输入错误,我想发送回主html文件,该表单在哪里,URL中有给定的数据,所以我可以说出问题所在。 But if I do this, the registration form will be closed, because it's again a display:none. 但是,如果这样做,将关闭注册表,因为它再次是display:none。 How can I set the div's display again from the php, after I send back the user? 回送用户后,如何从php再次设置div的显示? Sorry if it's complicated... 抱歉,很复杂...

I tried to call the js function in the php, after I send back with the header function, but it didn't work. 我用标头函数发送回去后,尝试在php中调用js函数,但是没有用。

HTML div: HTML div:

<div id="reg-Page">
 <!-- Form, and others -->
</div>

Call div: 致电div:

<button type="button" name="button" onclick="return regForm()">Signup</button>

js function: js函数:

 function regForm() {
  var logWindow = document.getElementById('reg-Page');
  if (logWindow.style.display != 'block') {
    logWindow.style.display = 'block';
  } else {
    logWindow.style.display = 'none';
  }
}

PHP file: PHP文件:

 if (empty($username) || empty($password) || empty($password2) || empty($email)) {
    ?>
      <script src="js/scripts.js" type="text/javascript">
        return regForm()
      </script>
    <?php
    header("Location: ../index.php?error=emptyfields&uid=".$username."&mail=".$email);
    exit();
  }

I tried to set the div display to block again, after the send back. 我尝试将div显示设置为在发送回之后再次阻止。

I tried to call the js function in the php, after I send back with the header function, but it didn't work. 我用标头函数发送回去后,尝试在php中调用js函数,但是没有用。

This is most like a race condition. 这最像比赛条件。 Your server is sending an html file with a script tag in it, and a div tag in it, but which loads first, and what parts of the DOM are accessible when the script runs, isn't clear with this method. 您的服务器正在发送一个HTML文件,该文件中包含一个脚本标签,其中包含一个div标签,但是该方法尚不清楚,该文件首先加载,并且在脚本运行时可以访问DOM的哪些部分。 At minimum you should do: 至少您应该这样做:

    ?>
      <script src="js/scripts.js" type="text/javascript">
        $(regForm);
      </script>
    <?php

That's assuming you have jQuery, using the $(document).ready() shorthand . 假设您使用$(document).ready()速记形式使用jQuery。 The idea here is the function doesn't run until the DOM is complete (ie the browser has parsed to the end of the document), and then your script has a shot at finding the div it wants to unhide. 这里的想法是该函数直到DOM完成(即浏览器已解析到文档的末尾)后才运行,然后您的脚本开始寻找要取消隐藏的div。

However, FYI, this is an antiquated way of doing things. 但是,仅供参考,这是一种过时的处理方式。 You should learn more about client/server programming (some commenters have provided links about this). 您应该了解有关客户端/服务器编程的更多信息(一些评论者提供了有关此链接)。 I would build the system more like a single-page app where the server had an API, and the client could send xhr requests based on the form, and take action based on the server's response. 我将构建一个类似于单页应用程序的系统 ,其中服务器具有API,客户端可以根据表单发送xhr请求,并根据服务器的响应采取措施。 This is completely possible with the php/mysql stack you're describing. 您所描述的php / mysql堆栈完全有可能。

Is it possible to show a div with an url? 是否可以显示带有网址的div?

I've added your title above here because I could help you with this, but did not really understand what the rest of your question was. 我在此处上方添加了您的标题,因为我可以为您提供帮助,但并没有真正理解您剩下的问题是什么。 Could you help us further by making your question more precise? 您能否通过使您的问题更加精确来进一步帮助我们?

Use the :target pseudo element selector in CSS. 在CSS中使用:target伪元素选择器。 This way you can listen for a hash or # in the URL and select an element with the id that matches the hash. 这样,您可以侦听URL中的哈希或# ,然后选择id与哈希匹配的元素。

And if possible use classes to hide or show elements to separate your HTML from your CSS. 并尽可能使用类来隐藏或显示元素,以将HTML与CSS分开。

So add CSS that specifies the states of the elements(default, target and hidden). 因此,添加用于指定元素状态(默认,目标和隐藏)的CSS In your JS switch over to changing the classes instead of the inline style properties. 在您的JS中切换到更改类而不是内联样式属性。 If you do that, you're missing out on some of the best CSS features. 如果这样做,您将错过一些最佳的CSS功能。

And in your PHP simply add a # with the id of the element you want to show to your redirect URL. 在您的PHP中,只需在您的重定向URL中添加一个# ,并将其与要显示的元素的ID相对应。 In your case #reg-Page . 在您的情况下#reg-Page

HTML 的HTML

<div id="reg-Page">
 <!-- Form, and others -->
</div>

CSS 的CSS

#reg-Page {
  display: none;
}

#reg-Page:target {
  display: block;
  /* Set your properties here */
}

.hidden {
  display: none;
}

JS JS

function regForm() {
  var logWindow = document.getElementById('reg-Page');
  if (!logWindow.classList.contains('hidden')) {
    logWindow.classList.add('hidden') 
   } else {
    logWindow.classList.remove('hidden')
  }
}

PHP 的PHP

<?php
// Add the ID of the div with add # before it at the end of the page.
header("Location: ../index.php/?error=emptyfields&uid=".$username."&mail=".$email.'#reg-Page');
?>

Let me know if I understood your question correctly and if this helps. 让我知道我是否正确理解了您的问题,是否有帮助。

In index.php you can check if an error message is set into the URL and use that information to output a bit of JS to trigger the function to enable to form when the page is ready: index.php您可以检查URL中是否设置了错误消息,并使用该信息输出一些JS来触发页面准备就绪时形成的功能:

<?php if (isset($_GET['error'])): ?>
    <script type="text/javascript">
        window.addEventListener("load", regForm);
    </script>
<?php endif; ?>

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

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