how to passing variable php_self, with one time submit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bengkel
Forum Newbie
Posts: 6
Joined: Wed Apr 11, 2012 4:21 am

how to passing variable php_self, with one time submit

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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?
bengkel
Forum Newbie
Posts: 6
Joined: Wed Apr 11, 2012 4:21 am

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

Post 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.
bengkel
Forum Newbie
Posts: 6
Joined: Wed Apr 11, 2012 4:21 am

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

Post 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
bengkel
Forum Newbie
Posts: 6
Joined: Wed Apr 11, 2012 4:21 am

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

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
bengkel
Forum Newbie
Posts: 6
Joined: Wed Apr 11, 2012 4:21 am

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

Post by bengkel »

Thanks for the tips, social_experiment. appreciate your help.
I found it very useful, since i'm just a beginner.

Regards,
Bengkel.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
bengkel
Forum Newbie
Posts: 6
Joined: Wed Apr 11, 2012 4:21 am

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

Post by bengkel »

Thank you Celauran, appreciate your help.
Your suggestion works for me.

Cheers,
Bengkel.
Post Reply