如何获取表单字段值,然后使用“填充”按钮后面的URL?

时间:2022-08-25 21:48:51

On my PHP web page, I want to have an input field where the user can enter a web address. I want to do a quick validation to make sure it actually looks like a web address. If good, I want to append that web address to the URL behind some buttons the user can click.

在我的PHP网页上,我想要一个输入字段,用户可以在其中输入网址。我想做一个快速验证,以确保它看起来像一个网址。如果好,我想将该网址附加到用户可以点击的某些按钮后面的URL。

For example, maybe I want to create something that lets the user enter a web address, then click buttons that send that address to the W3C CSS, HTML, and link-checking validators.

例如,我想创建一些允许用户输入网址的内容,然后单击将该地址发送到W3C CSS,HTML和链接检查验证器的按钮。

Also, I can't have the page refresh between the input field entry and clicking the buttons; clicking the buttons needs to automatically pick up the value, stuff the URL, and redirect to the destination page.

此外,我无法在输入字段条目和单击按钮之间刷新页面;单击按钮需要自动获取值,填充URL并重定向到目标页面。

Anyone know how to do this via standard HTML or PHP? I'd like to avoid JavaScript if possible.

任何人都知道如何通过标准的HTML或PHP来做到这一点?如果可能的话,我想避免使用JavaScript。

Thanks.

4 个解决方案

#1


HTML does not change during the duration of the page being viewed, and PHP only happens 'before' the page is viewed. Therefore, it is not achievable without Javascript.

HTML在查看页面的持续时间内不会发生变化,PHP只会在查看页面之前“发生”。因此,没有Javascript就无法实现。

#2


Why not cut out the middle man and just provide a link to the actual page onClick, so long as there's something in the text field? Yeah, you'll have to use javascript, but it'll make things faster and it will utilize more of the work that's already been done on the part of the W3C. Their URL scheme seems to be pretty easy to manipulate. Here's an example from the CSS validator.

为什么不切断中间人,只提供实际页面onClick的链接,只要文本字段中有东西?是的,你将不得不使用javascript,但它会让事情更快,它将利用W3C已经完成的更多工作。他们的URL方案似乎很容易操作。这是CSS验证器的一个例子。

http://jigsaw.w3.org/css-validator/validator?uri=[ENTER YOUR URL-ENCODED URL HERE]&profile=css21&usermedium=all&warning=1&lang=en

http://jigsaw.w3.org/css-validator/validator?uri= [在此处输入您的网址编码网址]&profile = css21&usermedium = all&warning = 1&lang = zh-CN

#3


chacha102 is right.

chacha102是对的。

the work you need to get done is required to be done by the client (=browser)

您需要完成的工作需要由客户端完成(=浏览器)

javascript would be able to do this very easily. for sure you can create some request after the fields content has changed. which would require some sort of client-side execution.

javascript可以很容易地做到这一点。确保您可以在字段内容更改后创建一些请求。这需要某种客户端执行。

your server-side code (php) cannot know about the actions that have been taken on the client-side without having communicated with the browser.

你的服务器端代码(php)无法知道在没有与浏览器通信的情况下在客户端采取的操作。

to let the browser communicate with your php-server, there are two fundamental ways... some kind of AJAX or reloading the page - both of which you don't want.

让浏览器与你的php服务器通信,有两种基本方式......某种AJAX或重新加载页面 - 这两种方法都是你不想要的。

not achievable, sorry.

不能实现,抱歉。

regards

#4


One solution, if I understand the question correctly, might be to use a form with :

一个解决方案,如果我理解正确的问题,可能是使用表格:

  • one input type=text field for the URL
  • 一个输入类型= URL的文本字段

  • one submit button per validator ; all having the same name
  • 每个验证器一个提交按钮;都具有相同的名称

Something like this :

像这样的东西:

<html>
<head>
    <title>Test ^^</title>
</head>
<body id="body-front">
    <form method="get" action="">
        <input type="text" value="" name="url" />
        <input type="submit" name="validator" value="HTML Validator" />
        <input type="submit" name="validator" value="CSS Validator" />
    </form>
</body>
</html>

The form posts (gets, actually, here ^^ ) on itself ; which means, at the beginning of the PHP script, you can check if it has been submitted.

表格发布(实际上,这里是^^);这意味着,在PHP脚本的开头,您可以检查它是否已提交。

If it has, you have to

如果有,你必须

  • check if the URL is OK
  • 检查URL是否正常

  • detect which button was clicked (through it's value attribute)
  • 检测单击了哪个按钮(通过它的value属性)

  • redirect to the right URL
  • 重定向到正确的URL

For instance, you could put this on top of the .php file containing the form I just posted :

例如,您可以将它放在包含我刚发布的表单的.php文件的顶部:

<?php
    if (isset($_GET['url'])) {
        // TODO : check the URL
        // if not... do what you have to (like re-display the form)
        if ($_GET['validator'] == "HTML Validator") {
            header('Location: http://validator.w3.org/check?verbose=1&uri=' . urlencode($_GET['url']));
            die;
        } else if ($_GET['validator'] == "CSS Validator") {
            header('Location: http://jigsaw.w3.org/css-validator/validator?profile=css21&warning=0&uri=' . urlencode($_GET['url']));
            die;
        }
        // Not one the buttons we know => re-display the form
    }
?>

It does just what I said :

它就是我所说的:

  • has the form been submitted ?
  • 表格已提交?

  • is the URL OK (that's your job to say ;-) )
  • 是好的URL(这是你的工作;-))

  • which button has been choosen ?
  • 选择了哪个按钮?

  • redirect to te right URL
  • 重定向到右侧URL

With that, your user submits the form, and it feels to him like he is directly sent to the website of the validator he choose.

这样,您的用户就会提交表单,并且他觉得他直接被发送到他选择的验证器的网站。

Without any kind of JS, I don't see another way...

没有任何类型的JS,我看不到另一种方式......

(Note : I tested this only on Firefox ; would be better to test with other browsers ^^ particularly with the "all submit buttons have the same name" thing)

(注意:我只在Firefox上进行了测试;最好用其他浏览器进行测试^^特别是“所有提交按钮具有相同名称”的东西)


Hope this helps... Sorry if I didn't understand the question...

希望这有帮助......对不起,如果我不明白这个问题......

#1


HTML does not change during the duration of the page being viewed, and PHP only happens 'before' the page is viewed. Therefore, it is not achievable without Javascript.

HTML在查看页面的持续时间内不会发生变化,PHP只会在查看页面之前“发生”。因此,没有Javascript就无法实现。

#2


Why not cut out the middle man and just provide a link to the actual page onClick, so long as there's something in the text field? Yeah, you'll have to use javascript, but it'll make things faster and it will utilize more of the work that's already been done on the part of the W3C. Their URL scheme seems to be pretty easy to manipulate. Here's an example from the CSS validator.

为什么不切断中间人,只提供实际页面onClick的链接,只要文本字段中有东西?是的,你将不得不使用javascript,但它会让事情更快,它将利用W3C已经完成的更多工作。他们的URL方案似乎很容易操作。这是CSS验证器的一个例子。

http://jigsaw.w3.org/css-validator/validator?uri=[ENTER YOUR URL-ENCODED URL HERE]&profile=css21&usermedium=all&warning=1&lang=en

http://jigsaw.w3.org/css-validator/validator?uri= [在此处输入您的网址编码网址]&profile = css21&usermedium = all&warning = 1&lang = zh-CN

#3


chacha102 is right.

chacha102是对的。

the work you need to get done is required to be done by the client (=browser)

您需要完成的工作需要由客户端完成(=浏览器)

javascript would be able to do this very easily. for sure you can create some request after the fields content has changed. which would require some sort of client-side execution.

javascript可以很容易地做到这一点。确保您可以在字段内容更改后创建一些请求。这需要某种客户端执行。

your server-side code (php) cannot know about the actions that have been taken on the client-side without having communicated with the browser.

你的服务器端代码(php)无法知道在没有与浏览器通信的情况下在客户端采取的操作。

to let the browser communicate with your php-server, there are two fundamental ways... some kind of AJAX or reloading the page - both of which you don't want.

让浏览器与你的php服务器通信,有两种基本方式......某种AJAX或重新加载页面 - 这两种方法都是你不想要的。

not achievable, sorry.

不能实现,抱歉。

regards

#4


One solution, if I understand the question correctly, might be to use a form with :

一个解决方案,如果我理解正确的问题,可能是使用表格:

  • one input type=text field for the URL
  • 一个输入类型= URL的文本字段

  • one submit button per validator ; all having the same name
  • 每个验证器一个提交按钮;都具有相同的名称

Something like this :

像这样的东西:

<html>
<head>
    <title>Test ^^</title>
</head>
<body id="body-front">
    <form method="get" action="">
        <input type="text" value="" name="url" />
        <input type="submit" name="validator" value="HTML Validator" />
        <input type="submit" name="validator" value="CSS Validator" />
    </form>
</body>
</html>

The form posts (gets, actually, here ^^ ) on itself ; which means, at the beginning of the PHP script, you can check if it has been submitted.

表格发布(实际上,这里是^^);这意味着,在PHP脚本的开头,您可以检查它是否已提交。

If it has, you have to

如果有,你必须

  • check if the URL is OK
  • 检查URL是否正常

  • detect which button was clicked (through it's value attribute)
  • 检测单击了哪个按钮(通过它的value属性)

  • redirect to the right URL
  • 重定向到正确的URL

For instance, you could put this on top of the .php file containing the form I just posted :

例如,您可以将它放在包含我刚发布的表单的.php文件的顶部:

<?php
    if (isset($_GET['url'])) {
        // TODO : check the URL
        // if not... do what you have to (like re-display the form)
        if ($_GET['validator'] == "HTML Validator") {
            header('Location: http://validator.w3.org/check?verbose=1&uri=' . urlencode($_GET['url']));
            die;
        } else if ($_GET['validator'] == "CSS Validator") {
            header('Location: http://jigsaw.w3.org/css-validator/validator?profile=css21&warning=0&uri=' . urlencode($_GET['url']));
            die;
        }
        // Not one the buttons we know => re-display the form
    }
?>

It does just what I said :

它就是我所说的:

  • has the form been submitted ?
  • 表格已提交?

  • is the URL OK (that's your job to say ;-) )
  • 是好的URL(这是你的工作;-))

  • which button has been choosen ?
  • 选择了哪个按钮?

  • redirect to te right URL
  • 重定向到右侧URL

With that, your user submits the form, and it feels to him like he is directly sent to the website of the validator he choose.

这样,您的用户就会提交表单,并且他觉得他直接被发送到他选择的验证器的网站。

Without any kind of JS, I don't see another way...

没有任何类型的JS,我看不到另一种方式......

(Note : I tested this only on Firefox ; would be better to test with other browsers ^^ particularly with the "all submit buttons have the same name" thing)

(注意:我只在Firefox上进行了测试;最好用其他浏览器进行测试^^特别是“所有提交按钮具有相同名称”的东西)


Hope this helps... Sorry if I didn't understand the question...

希望这有帮助......对不起,如果我不明白这个问题......