tags:

views:

550

answers:

4

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.

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.

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.

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

Thanks.

+1  A: 

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.

Chacha102
A: 

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.

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

Robert Elwell
A: 

chacha102 is right.

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.

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

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.

not achievable, sorry.

regards

Atmocreations
A: 

Hi,

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

  • one input type=text field for the 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.

If it has, you have to

  • check if the URL is OK
  • detect which button was clicked (through it's value attribute)
  • redirect to the right URL

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

<?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&amp;uri=' . urlencode($_GET['url']));
            die;
        } else if ($_GET['validator'] == "CSS Validator") {
            header('Location: http://jigsaw.w3.org/css-validator/validator?profile=css21&amp;warning=0&amp;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 ;-) )
  • which button has been choosen ?
  • redirect to te right 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...

(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)


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

Pascal MARTIN