|
Posted by Rik on 08/15/06 22:46
Chris wrote:
> "Rik" <luiheidsgoeroe@hotmail.com> wrote in message
> news:<8e84e$44e23d2f$8259c69c$14497@news1.tudelft.nl>...
>>
>> Well, if you're having a problem with the javascript, post in
>> comp.lang.javascript,
>> js has absolutely nothing to do with the PHP which created it.
>> Judging by your code:
>
> I don't understand your statement - "js has absolutely nothing to do
> with the PHP which created it". My problem isn't the js - it works
> okay on a non-php generated form - I have used js to validate html
> forms in the past with no problem.
Then check your HTML & JS source again. There are errors in it. As soon as
the page arrives it's just that, a page, which works exactly the same as if
you had typed the static HTML/JS by hand.
> The question is how to work with
> PHP and js together - a commonly asked question in forums.
Which will always be given the answer I gave you.
> There are
> ways to pass the variables back and forth, etc. which I have used
> elsewhere in my application.
Not really, unless you use AJAX and the like to start seperate request to
the server. PHP on the server and JS on the clients UA. They cannot
communicate directly. All PHP recieves are HTTP requests in this case.
> But I haven't seen anything regarding
> form validation when the form is PHP and the validation is js.
Because there is no difference.
>>> function ValidateForm(upload)
>>> {
>>> if(IsEmpty(upload.title))
>>> {
>>> alert('You have not entered a title!')
>>> form.title.focus();
>>> return false;
>>> }
>>>
>>> return true;
>>> }
>>
>>> <form enctype="multipart/form-data" action="upload.php"
>>> name="upload" id="upload" method="post" onsubmit="javascript:return
>>> ValidateForm(upload)">
>>
>>> <input name="title" id="title" type="text" size="40"
>>> maxlength="50" />
>>
>> 1. Lose the 'javascript:'
>> 2. 'upload' isn't set, use "return ValidateForm(this)"
>
> I had tried that before (when I had more complicated js) - and I
> tried it again - no help.
Then maybe the good people here in c.l.javascript can help you. MNaybe it's
just as well it's also in c.l.php, let's hope people will search for this
topic, and understand it, so I won't feel obliged to explain it for the
hundredth time.
>> And then I still doubt 'upload.title' and 'form.title.focus()' are
>> valid, but hey, I know very little js.
>>
>>> Is there something
>>> special I need to do to make the Javascript work before the PHP? Is
>>> there a good way to do client side validation with PHP?
>>
>> No, PHP outputs javascript as pure text, and has absolutely nothing
>> to do with it's workings on the clients computer. Furthermore client
>> side validation with PHP is impossible as it runs on the server, not
>> on the clients computer, you can only submit the form to it, check
>> the values, and
>> report them back in a new HTML page (not neccesarily another script,
>> could easily be the same).
>
> That's my concern as I know that PHP is server-side and processes
> before data goes to the browser. So - how to intercept the data as it
> goes from the browser to the server?
You don't 'intercept' the data, you prevent the sending of a request. Pure
js, no PHP involved, PHP cannot influence anything as soon as the page is
sent, and will only go to work as soon as it gets another request by HTTP.
> I'm sure this is a fairly simple process, since there are tons of PHP
> forms out there that get validated.
Yup, by js on the client side, and php on the server side, or none, or a
combination of both.
> Perhaps incorporate the js into
> the processing page at the very beginning?
I sincerely doubt an error in js or your html will be solved by moving the
script tags around.
> //check that form has been submitted
> if (isset($_POST['uploadfile'])) { //begin check for submission
> Validate form elements
> and show js alerts here
Impossible, the form is already submitted.
> (how to redirect user
> back to form and elements for revisions? Maybe I should keep the
> processing on the same page as the code.
A colored border around element's which are not correctly filled, and
echoing usefull hints is the standard way to go.
> //check if file uploaded
> if (isset($_FILES['fileupload']['name'])) { // begin check for
> uploaded file What to do if form is valid:
> 1.create path and move temp file to permanent location
> 2.insert data into db
> 3.display success page
> }else{
> File not uploaded - try again!
> }
> } else { //end submission and validation check
> Show form
> }
>>
> Does this look like reasonable workable logic?
That's a workable logic, which has nothing to do with js validation on the
client side.
Grtz,
--
Rik Wasmus
Navigation:
[Reply to this message]
|