Form Variable

Form Variable

am 19.01.2006 22:46:45 von Nick Lazar

Hi there,

I am trying to extract a form field into a variable within the same form for
the purpose of generating a unique tracking number. The user makes a choice
from a drop down menu, and two letters that relate to this choice are
inserted into the PHP generated number.

I have tried to set this variable using $_POST but it doesn't work, (I
assume because when submit is clicked it can't access the $_POST result?)

Can anybody tell me how to achieve this?

Thanks,

Nick.

Re: Form Variable

am 19.01.2006 22:55:30 von Adam Plocher

Let me see if I understand you correctly.. you want to have a PHP
generated value returned from the web server when a user changes a form
value (but before they submit the form)?

If this is correct, you'll need to do this with either a auto-postback
when you leave that field or put a button on your page that the user
has to click to generate a new number. Regardless, you'll need to post
back to the web server in order to have the web server generate a new
value. This could be a good place to use AJAX, too.

You could have JavaScript do it for you, but what is it's uniqueness
based on? If it's based on a database value, then javascript probably
wouldn't be the best.

Re: Form Variable

am 19.01.2006 23:14:16 von Nick Lazar

Adam,

Thanks for your response.

Yes, you've got the gist of what I am trying to do.

Basically I'm generating the number using the following script:

//set the random id length
$random_id_length = 8;

//generate a random id and store it in $rnd_id
$rnd_id = (uniqid(rand(),1));

//remove any slashes that might have come
$rnd_id = strip_tags(stripslashes($rnd_id));

//Removing any . or / and reversing the string
$rnd_id = str_replace(".","",$rnd_id);
$rnd_id = strrev(str_replace("/","",$rnd_id));

//finally I take the first 10 characters from the $rnd_id
$rnd_id = substr($rnd_id,0,$random_id_length);

The user then chooses what level of support they want, and a two letter
option from the drop down needs to go in front of the number, (I'm using the
switch, case method to drop the right letters in). The entire ID is then
inserted into MySQL & e-mailed to the user.

It seemed like a good idea until I ran into the problem I have outlined!

I'll experiment with your suggestions.

Regards,

Nick.

Re: Form Variable

am 19.01.2006 23:33:55 von Shion

Nick Lazar wrote:
> Adam,
>
> Thanks for your response.
>
> Yes, you've got the gist of what I am trying to do.
>
> Basically I'm generating the number using the following script:
>
> //set the random id length
> $random_id_length = 8;
>
> //generate a random id and store it in $rnd_id
> $rnd_id = (uniqid(rand(),1));
>
> //remove any slashes that might have come
> $rnd_id = strip_tags(stripslashes($rnd_id));
>
> //Removing any . or / and reversing the string
> $rnd_id = str_replace(".","",$rnd_id);
> $rnd_id = strrev(str_replace("/","",$rnd_id));
>
> //finally I take the first 10 characters from the $rnd_id
> $rnd_id = substr($rnd_id,0,$random_id_length);
>
> The user then chooses what level of support they want, and a two letter
> option from the drop down needs to go in front of the number, (I'm using the
> switch, case method to drop the right letters in). The entire ID is then
> inserted into MySQL & e-mailed to the user.
>
> It seemed like a good idea until I ran into the problem I have outlined!
>
> I'll experiment with your suggestions.

Why not generate the code in the page that processes the users request and add
the stuff selected

$ID=$firstchar.$secondchar.$rnd_id;

If you want that the user sees the ID while they choose the two letters, then
you need to use javascript to manipulate the HTML page or else you need to
resend the whole form each time you make a change which is a bad idea as it
generates quite a lot of traffic.


//Aho

Re: Form Variable

am 19.01.2006 23:36:31 von Adam Plocher

Hmm.. you know, JavaScript could probably do all this, and that way you
could generate this value without doing a postback. You can also have
JavaScript populate a hidden form variable to send back to your
database.

Does something like this work?

Note, to test this, just put this code into a html file and type
something in the text box.. when you leave the text box it should
generate a 10 digit # that is prefixed by whatever you type into the
input box. It will also set a hidden form value, so if you were to
submit this form, php should be able to pick it up.












Output:




I guess one problem is that it COULD theoretically duplicate an ID. It
won't be a definite unique ID. If you have to have it check against a
DB, AJAX might be the best solution, but that's a little trickier.

If it HAS to be unique, let me know and I'll help you out with an AJAX
solution.

Re: Form Variable

am 20.01.2006 00:34:37 von Nick Lazar

Thanks very much for those suggestions. It's given me a few ideas that I'll
try out in the morning.

The ID does need to be unique, and although I have nothing against
JavaScript, it is quite possible that a number of the target users may have
it turned off in their browser, so my first choice is not to rely on it too
heavily, but I hadn't thought of using it for this, so will try it out....

Cheers,

Nick.


On 19/1/06 10:36 PM, in article
1137710191.286660.226560@g47g2000cwa.googlegroups.com, "Adam Plocher"
wrote:

> Hmm.. you know, JavaScript could probably do all this, and that way you
> could generate this value without doing a postback. You can also have
> JavaScript populate a hidden form variable to send back to your
> database.
>
> Does something like this work?
>
> Note, to test this, just put this code into a html file and type
> something in the text box.. when you leave the text box it should
> generate a 10 digit # that is prefixed by whatever you type into the
> input box. It will also set a hidden form value, so if you were to
> submit this form, php should be able to pick it up.
>
>
>
>
>
>
>
>
>


>


> Output:
>

>
>
>
> I guess one problem is that it COULD theoretically duplicate an ID. It
> won't be a definite unique ID. If you have to have it check against a
> DB, AJAX might be the best solution, but that's a little trickier.
>
> If it HAS to be unique, let me know and I'll help you out with an AJAX
> solution.
>