Jump to content

basic php submit and validation


jihmantiquilla

Recommended Posts

gud day,

 

i'm new in here phpfreaks and also new into php programming, javascript. but an experienced programmer with regards to .net, i'am juz confused with regards to the submission and validation of forms. when i started to submit the page. the values on the pass and pass2 textboxes always disapper.:(:(

 

this is my code

javascript

function checkTextboxes()
{
    f=document.form1;
    f.command.value="Add";
    f.submit;
}

 

html

<input name="btnSubmit" type="submit" value="Submit" onclick="checkTextboxes()" style="position:absolute;left:211px;top:527px;z-index:27">

 

php

<?
if($_REQUEST['command']=='Add')
{
    $pass=$_REQUEST['txtPass'];
    $pass2=$_REQUEST['txtPass2'];
?>
<div id="text" style="position:absolute; overflow:hidden; left:240px; top:280px; width:180px; height:20px; z-index:6"><div class="wpmd">
<div><font face="Arno Pro Caption" color="red">
<?
    if($pass!=$pass2)
    {
        echo("Password values are different");
    }
?>
</font></div>
</div></div> 
<?
}
?>


another question,.. is there is a way where in i can manage the events that occur to a control?? like textchange in textboxes.

any comments and suggestion is much appreciated

Link to comment
Share on other sites

The only time $variables exist on a form is between the time the user requests that page and the time the PHP parser has finished processing your page. After that point if you have not stored the value in some manner that would allow it to persist the value is lost.

<?php
if($_REQUEST['command']=='Add')
{
    $pass=$_REQUEST['txtPass'];
    $pass2=$_REQUEST['txtPass2'];
/*
At this point you must either:
echo $pass back into a hidden input on a form, so the next time the user clicks submit it still exists.
store it in a variable that does persist (such as the $_SESSION superglobal array)
store it in a flat file system
store it in a database
*/
?>

 

It's very different to working with a .NET desktop application. In that situation obviously you have access to any variables in scope at all times. With PHP there is no ability to work in that way with the site being a multi-user experience obviously single instances of $variables become irrelevant. The most similar likeness is the $_SESSION array which allows you to store information for each individual on the site.

 

Handling events on input items I believe it possible but it's JavaScript related and alas I know nothing about JavaScript.

Link to comment
Share on other sites

Try showing the contents of $_GET or $_POST (<form method='get' or 'post'>) using print_r();  I usually recommend staying away from $_REQUEST, which is rather generic and includes both.

 

As far as managing events for form elements, check out prototype (prototypejs.org).  It makes unobtrusive javascript easier, and better allows you to separate display from functionality.

Link to comment
Share on other sites

what is your forms action set to? is it set to the correct page?

 

i haven't put any thing to the action method. since i have the event on the button onclick.

 

Try showing the contents of $_GET or $_POST (<form method='get' or 'post'>) using print_r();  I usually recommend staying away from $_REQUEST, which is rather generic and includes both.

does it really recommended not to use $_REQUEST. isn't that the print_r() is also the same with echo()??

 

 

It's very different to working with a .NET desktop application. In that situation obviously you have access to any variables in scope at all times. With PHP there is no ability to work in that way with the site being a multi-user experience obviously single instances of $variables become irrelevant. The most similar likeness is the $_SESSION array which allows you to store information for each individual on the site.

if i into using $_SEESION array where in i can store the values and retrieve it any time. does it always recommended to use $_SESSION in a simple form submission and validation??

 

 

 

 

 

Link to comment
Share on other sites

echo and print_r are different, print_r will show you the contents of an array or an object, including $_GET and $_POST.  The reason I don't like $_REQUEST is because it contains $_GET, $_POST, and $_COOKIES all in one.  I believe that $_GET requests should not allow a user to modify data (isn't this part of the http spec?)

Link to comment
Share on other sites

i had also a problem with regards to fetching data's on my sql statement. it seems the value tends to be always '0' knowing that in the sql statement it returns the value 1, and if i try to display it using print_r/echo/ ders no values that is being displayed. do i write my code right??

 

 

<?
if($_REQUEST['command']=='checkuser')
{
    $username=$_REQUEST['txtusername'];
    $result=mysql_query("select count(UserName) as User from user where UserName='$username'");
    $row=mysql_fetch_field($result) or die(mysql_error());
?>
<div id="text" style="position:absolute; overflow:hidden; left:240px; top:202px; width:180px; height:20px; z-index:6"><div class="wpmd">
<div><font face="Arno Pro Caption" color="red">
<?
    if($username!="")
    {
        if($row->User>=1)
        {
            print_r($row->User);        
            //echo("Username already exist");
        }
        else if($row->User==0)
        {
            //echo("Username Available");
        
            print_r($row->User);
        }
    }
    else
    {
        echo("No values found in username");
    }
?>
</font></div>
</div></div>
<?
}
?>

Link to comment
Share on other sites

I tend to use a library like Zend Framwork when working with forms, abstracts much of the work. Since you are a veteran .net it will be easy for you to get the idea.

But, as a basic thing, I used to write my forms like that:

<form method='post' action=''>
  <input type='text' name='myname' value='<?php echo $_POST['myname']; ?>' />
  ...
  ...
  ...
  <input type='submit' value='save' />
</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.