简体   繁体   English

在新页面中动态生成的HTML(服务器端)

[英]Dynamically generated HTML (Server-side) in new page

For learning purposes, I am building a simple web app that allows users to write an introduction page for themselves. 出于学习目的,我正在构建一个简单的Web应用程序,允许用户自己编写简介页面。 I am very confused about dynamically generated HTML on the server-side and how it connects to the client-side. 我对服务器端动态生成的HTML及其如何连接到客户端感到非常困惑。 I am currently using PHP and Mustache templates to generate the about page HTML on the server side and that works fine. 我目前正在使用PHP和Mustache模板在服务器端生成About页面HTML,并且工作正常。

Lets say I type in the URL: localhost/intro.html 可以说我输入URL:localhost / intro.html

On intro.html there is a button and if I click it then the browser would bring me to a new URL (localhost/intro.html/Adam) with the introduction information of a user, lets just say "Adam". 在intro.html上有一个按钮,如果我单击它,浏览器会将我带到带有用户介绍信息的新URL(localhost / intro.html / Adam),请说“ Adam”。

From my understanding, this should send a request to the server to generate an about HTML page with information about Adam and send that HTML page back to the browser. 根据我的理解,这应该向服务器发送请求,以生成包含有关Adam的信息的HTML页面,并将该HTML页面发送回浏览器。

What I don't understand is what it would look like in HTML, JS (JQuery), and PHP. 我不了解的是HTML,JS(JQuery)和PHP中的外观。 Again, I can generate the HTML on the server side just fine, but how would clicking a button on localhost/intro.html change the page to localhost/intro.html/Adam ? 同样,我可以在服务器端生成HTML,但是单击localhost / intro.html上的按钮如何将页面更改为localhost / intro.html / Adam? How would my PHP code detect the page localhost/intro.html/Adam and know to generate HTML for it? 我的PHP代码将如何检测localhost / intro.html / Adam页并知道为其生成HTML? What does the code look like and am I missing some concepts? 代码看起来像什么,我缺少一些概念吗?

Any direction, sample code or tutorials would be much appreciated. 任何方向,示例代码或教程将不胜感激。 All I can find is strictly PHP tutorials. 我只能找到严格的PHP教程。 Thank you! 谢谢!

Okay, with your knowledge of databases and queries, you can do this... 好的,只要了解数据库和查询,就可以执行此操作...

Create your main page... we will call it index.php . 创建您的主页...我们将其称为index.php

1) Put this js in your <head> tag 1)将此js放在您的<head>标记中

<script type="text/javascript">
function MM_jumpMenuGo(objId,targ,restore){ //v9.0
  var selObj = null;  with (document) { 
  if (getElementById) selObj = getElementById(objId);
  if (selObj) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0; }
}
</script>

2) Create a form with a dropdown that has dynamically generated options and a submit button, inside a repeat region. 2)在重复区域内创建一个带有下拉菜单的表单,该下拉列表具有动态生成的选项和一个提交按钮。

3) The values for that dropdown would be something like <option value="/<?php echo $row['username'] ?>"><?php echo $row['username'] ?></option> 3)该下拉菜单的值类似于<option value="/<?php echo $row['username'] ?>"><?php echo $row['username'] ?></option>

4) Your submit button will call the javascript to send you to the page you want 4)您的提交按钮将调用javascript以将您发送到您想要的页面
<input type="button" name="go_button" id= "go_button" value="Go" onClick="MM_jumpMenuGo('jumpMenu','parent',0)">

Completed main page... 完成的主页...

<form name="form1">
  <select name="name" id="name">
    <option selected>Please make a selection</option>
    <?php do { ?>
    <option value="/<?php echo $row['username'] ?>"><?php echo $row['username'] ?></option>
    <?php } while($row = $query->fetch(PDO::FETCH_ASSOC)) ?>
  </select>
<input type="button" name="go_button" id= "go_button" value="Go" onClick="MM_jumpMenuGo('select14','parent',0)">
</form>

Then we start on your "target" page... we'll call it results.php 然后,我们从您的“目标”页面开始...我们将其称为results.php

1) Query the database for the username being passed from the main page. 1)在数据库中查询从主页传递的用户名。

$name = $_GET['name']; // this is the name of the dropdown on the main page
SELECT * FROM mytable WHERE username=:name

2) Make sure you bind your parameters $query->bindValue(':name', $name, PDO::PARAM_STR); 2)确保绑定参数$query->bindValue(':name', $name, PDO::PARAM_STR);

3) Then you can echo the information you want to display like this... <?php echo $row['fieldname'] ?> 3)然后,您可以像这样<?php echo $row['fieldname'] ?>显示的信息... <?php echo $row['fieldname'] ?>

Finally, the .htaccess file... 最后, .htaccess文件...

Create a rewrite rule to handle this... 创建一个重写规则来处理此问题...

RewriteEngine On    # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f # if this is not a real file
RewriteCond %{REQUEST_FILENAME} !-d # if this is not a real directory
RewriteRule    ^([A-Za-z0-9_-]+)$    results.php?name=$1    [NC,L]    # Handle page requests

And you're done. 这样就完成了。

Happy Coding ! 编码愉快!

Note: The code I use is pdo_mysql . 注意:我使用的代码是pdo_mysql You can get more information here 您可以在此处获取更多信息

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

相关问题 在新窗口中显示服务器端生成的HTML - Show the server-side generated HTML in a new window 如何将服务器端生成的新页面加载到PhoneGap或Cordova应用程序中? - How to load new pages generated server-side into a PhoneGap or Cordova app? HTML将图像的内容设置为服务器端脚本生成的内容 - HTML set an image's content to content generated by a server-side script 呈现HTML + Javascript服务器端 - Rendering HTML+Javascript server-side “操纵” html文件(服务器端),并将其内容插入html主页(*无* PHP) - “Manipulate” html files (server-side) and insert their content in main html page (*without* PHP) 服务器端呈现的HTML-&gt; JS表? - Server-side rendered HTML -> JS Table? Javascript获取页面源(服务器端) - Javascript Get Page Source (server-side) 使用JavaScript和Google App Engine中的服务器端Python代码动态生成客户端HTML表单控件 - Dynamically generate client-side HTML form control using JavaScript and server-side Python code in Google App Engine 如何使用expressJ将JS,CSS和其他服务器端实用程序包含到NodeJ中的HTML页面中? - How to include JS, CSS and other server-side utils to a HTML page in NodeJs using expressJs? 服务器端(Java)上的HTML页面内是否可能调用JavaScript代码? - It is a possible call JavaScript code inside HTML page on server-side (Java)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM