Page 1 of 1

how to passing variable php_self, with one time submit

Posted: Wed Apr 11, 2012 4:24 am
by bengkel
Hello,
I'm new member and newbie in PHP, i'm making php coding like this: (try.php)

Code: Select all

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>
    <body>
       <form action="<?php echo $_SERVER['PHP_SELF']; echo '?date1=';echo $_POST['inputField']; echo '&date2='; echo $_POST['inputField2']; ?>" method="post">
          <label for="DATE1">DATE1 </label><input type="text" size="12" name="inputField"  id="inputField"  value=<?php if(isset($_POST['inputField'])) {echo $_POST['inputField'];} else{echo date("Y-m-d");} ?> >
          <label for="DATE2">DATE2 </label><input type="text" size="12" name="inputField2" id="inputField2" value=<?php if(isset($_POST['inputField2'])){echo $_POST['inputField2'];}else{echo date("Y-m-d");} ?> >
          <input type="submit" name="submit" value="Submit" />
       </form>
    </body>
    </html>
when running try.php, I would like the following:
1. fill in DATE1 with such date 2012-04-01
2. fill in DATE2 with such date 2012-04-02
3. when i hit submit button , I want the result to be:
url address changes to be: try.php?date1=2012-04-01&date2=2012-04-02

what happens now is:
1. I fill in DATE1 with such date 2012-04-01
2. I fill in DATE2 with such date 2012-04-02
3. when I press submit, url address changes to be: try.php?date1=&date2=,
but strangely, when I press submit button once again, the result is correct , it become try.php?date1=2012-04-01&date2=2012-04-02

Please help me, sorry if my english is not good,
thank you very much for your help.

regards,
Bengkel

Re: how to passing variable php_self, with one time submit

Posted: Wed Apr 11, 2012 5:56 am
by social_experiment
bengkel wrote:but strangely, when I press submit button once again, the result is correct , it become try.php?date1=2012-04-01&date2=2012-04-02
When the form is first loaded, the $_POST values is not set (hasn't been entered) that's why the url is ?date1=&date2= when you click on it. The form goes to the page that is present within "action" when the page is created ; On the second click those values have been set so they display.

Have a look a this url; might assist with the issue you wish to resolve
http://stackoverflow.com/questions/6210 ... s-in-a-url

Re: how to passing variable php_self, with one time submit

Posted: Wed Apr 11, 2012 6:14 am
by Celauran
Are those really the only two fields in your form? If so, why are you using POST when it looks like GET is what you're really after?

Re: how to passing variable php_self, with one time submit

Posted: Wed Apr 11, 2012 10:49 pm
by bengkel
Hi Celauran ,
Thank you for your answer,yes it is really only two fields in my form, so i can see what's wrong with the coding.

when you suggest me to use _GET function, i change the code into

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; echo '?date1=';echo $_GET['inputField']; echo '&date2='; echo $_GET['inputField2']; ?>" method="post">

the result become:
1. try.php?date1=&date2=
2. no matter when i fill the date1 and date2 with another date entry, the result will permanently become above, try.php?date1=&date2=

is there any code to change in order to get result ( try.php?date1=2012-04-01&date2=2012-04-02 )? with one click on submit button ,not twice click on submit button?

thank you for your kind help,
Regards,
Bengkel.

Re: how to passing variable php_self, with one time submit

Posted: Wed Apr 11, 2012 10:55 pm
by bengkel
Hi social_experiment,
Thank you for your answer, if you copy paste my coding, into try.php and running it, the form will display date1 with today's date and date2 with today date too, so i only have to click submit, i expect the result will directly become, try.php?date1=2012-04-12&date2=2012-04-12 in your browser address bar, instead of this (try.php?date1=&date2=)

I have take a look on your suggestion page in http://stackoverflow.com/questions/6210 ... s-in-a-url
still i dont find the answer.

thank you for your help,
Regards,
Bengkel

Re: how to passing variable php_self, with one time submit

Posted: Thu Apr 12, 2012 3:14 am
by bengkel
I just try changed the code into

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; echo $_GET['inputField']; echo $_GET['inputField2']; ?>" method="get">
		<label for="DATE1">DATE1 </label><input type="text" size="12" name="inputField"  id="inputField"  value=<?php if(isset($_GET['inputField'])) {echo $_GET['inputField'];} else{echo date("Y-m-d");} ?> >
		<label for="DATE2">DATE2 </label><input type="text" size="12" name="inputField2" id="inputField2" value=<?php if(isset($_GET['inputField2'])){echo $_GET['inputField2'];}else{echo date("Y-m-d");} ?> >
	<input type="submit" name="submit" value="Submit" />
	</form>
i change the method into ="get" ,and also change _POST to _GET, and i get the result: try.php?inputField=2012-04-12&inputField2=2012-04-12&submit=Submit

and i think it just resolve my problem with the fixed variable with one time submit button.
Thank's for your comments.

Regards,
Bengkel

Re: how to passing variable php_self, with one time submit

Posted: Thu Apr 12, 2012 4:44 am
by social_experiment
You can also clean up the if statement within the value attribute by using the ternary operator (?:)

Code: Select all

<input type="text" size="12" name=inputField2" id="inputField2" value="<?php echo (isset($_GET['inputField2'])) ? $_GET['inputField2'] : date("Y-m-d"); ?>" />
A note about isset(): it only checks if a variable is set, not whether it contains anything

Re: how to passing variable php_self, with one time submit

Posted: Thu Apr 12, 2012 4:53 am
by bengkel
Thanks for the tips, social_experiment. appreciate your help.
I found it very useful, since i'm just a beginner.

Regards,
Bengkel.

Re: how to passing variable php_self, with one time submit

Posted: Thu Apr 12, 2012 5:50 am
by Celauran
bengkel wrote:Hi Celauran ,
Thank you for your answer,yes it is really only two fields in my form, so i can see what's wrong with the coding.

when you suggest me to use _GET function, i change the code into

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; echo '?date1=';echo $_GET['inputField']; echo '&date2='; echo $_GET['inputField2']; ?>" method="post">
No, that's still wrong.

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
That's it. GET will place the values in the URL for you; that's what GET does.

Re: how to passing variable php_self, with one time submit

Posted: Thu Apr 12, 2012 9:49 pm
by bengkel
Thank you Celauran, appreciate your help.
Your suggestion works for me.

Cheers,
Bengkel.