Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Submit with preg_match problem
Old 03-14-2009, 06:44 PM Submit with preg_match problem
Novice Talker

Posts: 5
Trades: 0
Hi,

I have a textfield i want to submit if the preg_match approves the text.

PHP Code:
if(isset($_POST['Cstring'])){
if(!
preg_match("/^[a-z]$/",$_POST['Name'],$Estring)){
 echo 
"Error";
    } 
When typing in anything other then whats allowed i get the error msg but the form still sends the information. What am i missing or doing wrong?
Codeme is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-14-2009, 07:30 PM Re: Submit with preg_match problem
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
I can't know without seeing the rest of your code, but it sounds like you haven't put the alternative reaction in else {} brackets.

- Steve
__________________
if($stevej == "helpful") { $talkupation += $user_power; }
stevej is offline
Reply With Quote
View Public Profile
 
Old 03-14-2009, 07:38 PM Re: Submit with preg_match problem
Novice Talker

Posts: 5
Trades: 0
Well thats all code i came up with except for the form,

PHP Code:
<formaction=""  method="post" >
    <
input type="text" name="Cstring" id="textfield" />
    <
input type="submit" name="change" value="Change now" />
     
</
form
How do i write a reaction for stopping form from submit the values i dont want.
Codeme is offline
Reply With Quote
View Public Profile
 
Old 03-14-2009, 08:19 PM Re: Submit with preg_match problem
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
So... like this?

PHP Code:

<?php
if ($_POST['change']) {
    if(!
preg_match("/[a-z]/",$_POST['Cstring'],$Estring)){
        echo 
"Error";
    } else {
        echo 
"It Worked!";
    }
}
?>
  
<form method="post" >
    <input type="text" name="Cstring" />
    <input type="submit" name="change" value="Change now" />
</form>
Because I'm pretty sure that you have some more code there... and I'm not totally sure I know what you're trying to do.

- Steve
__________________
if($stevej == "helpful") { $talkupation += $user_power; }
stevej is offline
Reply With Quote
View Public Profile
 
Old 03-14-2009, 08:26 PM Re: Submit with preg_match problem
Novice Talker

Posts: 5
Trades: 0
Sorry, ill try to explain better this time. Im trying to make my form return true or false depending on the preg_match. And i dont know how to do this in php.
Codeme is offline
Reply With Quote
View Public Profile
 
Old 03-14-2009, 09:55 PM Re: Submit with preg_match problem
stevej's Avatar
Professional Multitasker

Posts: 996
Location: Not positive
Trades: 0
And do you want the form to send some type of information somewhere? If not, then what I've given you ought to do the job.

- Steve
__________________
if($stevej == "helpful") { $talkupation += $user_power; }
stevej is offline
Reply With Quote
View Public Profile
 
Old 03-16-2009, 05:51 PM Re: Submit with preg_match problem
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I think you have missunderstood the way php works. Unlike JavaScript, which is run in the browser and can do things without reloading the page, php is a server side language (runs on the server, not in the browser). So for any php code to be executed you have to reload the page, go to a new page, submit a form etc. Anything that will send a new request to the server so that a page is loaded.

This means, you can not have php validate your form before it is sent. Instead you must send the form, retrieve the submited data and validate it. If the data is ok then do what you intentionally wanted to do with it (send as a mail, store in a file, print some message etc.). If the data does not validate you would normally give an error message, display the form again and ask the user to fill it in properly.

And just to make it clear, evan though I wrote "So for any php code to be executed you have to reload the page", this isn't just some downside of php in perticular. This is the way all serverside languages work, hence the name serverside.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Submit with preg_match problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25283 seconds with 12 queries