如何通过PHP编辑<h1>

时间:2015-12-08 22:31:26

标签: php html post

我想在不混合我的HTML和PHP的情况下实现这一点,因此没有内联PHP代码等。

我有一个非常简单的问题,至少看起来很简单。我知道你可以使用$ _POST通过PHP从元素中获取各种HTML值,例如从HTML页面获取年份$year = $_POST['year'];。我的问题是:

对于没有指定值的元素,您会怎么说?例如:

我如何更改内容:

<h1> Welcome to the Super Bowl Winner Search Years 2000 - 2015! <br />  <br />
                Select the year, see the winning team!</h1>
用户单击调用.php脚本的提交按钮后

<form action = "http://localhost/myPrograms/superBowl.php" method = "post" >

开始时,$year包含HTML元素年份的值<select name = "year" >(我的下拉框)

你会做一些与$h1 = $_POST['h1']类似的事吗?这似乎不对,我会对如何实际编辑<h1>的.innerHTML感到困惑吗?不只是创建一个新的h1

谢谢!

2 个答案:

答案 0 :(得分:1)

将所有三个文件放在同一目录中。然后在浏览器中查看super_bowl.php。

我正在使用一种简化的模板语言将字符串替换为html模板。

template.html

    <html>
        <head>
            <title>@@TITLE@@</title>
        </head>
        <body>
            <h1>@@HEADING@@</h1>
            @@CONTENT@@
            @@FORM@@
        </body>
    </html>

form.html

<form action="" method="get">
    <fieldset>
        <legend>Winner Picker</legend>
        <label for="year">Select the year to see the winning team:</label>
        <select name="year">
          <option value="2010">2010</option> 
          <option value="2011">2011</option>
          <option value="2012">2012</option>
        </select>
        <input type="submit" value="See Winner" />
    </fieldset>
</form>

super_bowl.php

<?php

$winners = array(
    '2010' => 'The Braves',
    '2011' => 'Blue Beards',
    '2012' => 'Fever Pitchers'
);

$year = isset($_GET['year']) && array_key_exists($_GET['year'], $winners) 
    ? $_GET['year']
    : null;

$winner = ! is_null($year)
    ? $winners[$year] 
    : null;

$template = file_get_contents('template.html');
$form     = file_get_contents('form.html');

$output = strtr($template, array(
    '@@TITLE@@' => 'Super Bowl Winners Search Years 2000 - 2015',
    '@@FORM@@' => $form,
    '@@HEADING@@' => 
        ! is_null($year) 
        ? 'Superbowl winner for the year: ' . $year 
        : 'Welcome to the Super Bowl Winner Search Years 2000 - 2015!',
    '@@CONTENT@@' => 
        ! is_null($winner)
        ? 'Were the ' . $winner . '.'
        : null
    ));

// Print page output
echo $output;

答案 1 :(得分:0)

我建议你放下:

       sregreq = OpenID::SReg::Request.new
       # required fields
       sregreq.request_fields(['email'], true)
       # optional fields
       sregreq.request_fields(['fullname'], false)
       oidreq.add_extension(sregreq)

但是,既然你坚持,请继续阅读......

只有这样,PHP才能在加载后通过AJAX与您的HTML进行交互,客户端也会请求服务器并获取信息。所以你需要在技术上做的是,你需要在页面加载后启动一个AJAX调用并获取信息。这是加载东西最疯狂的方式。

无论如何,你可以使用jQuery来做到这一点。是的,我会请求你使用jQuery来实现AJAX功能,这样你就不会搞砸浏览器了。

<h1> <?php echo (isset($_POST['h1'])) ? isset($_POST['h1'] : 'Welcome to the Super Bowl Winner Search Years 2000 - 2015! <br />  <br /> Select the year, see the winning team!' ; ?> </h1>

$("h1").load("/php-file.php?get=h1");

php-file.php

我真的想不出任何其他更好的方式回答这个问题。