I cant post my textbox value to another page - php

Hi this is my first aplication .It is self explained code. I think I have missed out something I am trying to change some chars to other but I think I couldnt .because I am just getting a blank page
this is control.php
<?php
function donustur($karakter){
$tk=array("ç","ş","ö");
$ik=array("c","s","o");
$ykarakter= str_replace($tk,$ik,$karakter);
return $ykarakter;
}
$ad=donustur($_POST['adi']);
$mesaj=donustur($_POST['mesaj']);
echo "ad : ".$ad;
echo "ad : ".$mesaj;
?>
this is index.php
<form action="control.php" method="post" enctype="text/plain">
adi : <input type="text" value="adi" size="10" /><br />
soyadı<textarea cols="30" rows="30"
wrap="virtual" maxlength="10" name="mesaj"></textarea><br />
<input type="submit" value="submit" />
</form>

You have at least two problems.
First, see the documentation for plain text form data:
Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer
PHP cannot interpret form data submitted as plain text.
Remove the enctype attribute to use the default encoding type (application/x-www-form-urlencoded).
The only time you should specify the enctype is when you are including file inputs in the form, in which case you need to use multipart/form-data.
Second, a form control can only be successful if it has a name. Add a name attribute to your text input.
<input type="text" name="adi" value="adi" size="10">
Finally, even with those problems, I don't think you should get a blank page. (I don't think any of the errors are critical enough to make PHP bail out). You should get output of:
ad : ad :
If you don't, then the odds are that the file is not being processed with PHP at all. Make sure that you are:
Running a web server that supports PHP
Loading the HTML documents by pointing your browser at http://yourserver/ and not something like file:///c:/foo/bar/index.php

I cleaned it up as best I could considering I don't understand the base language. Hope this helps!
control.php
<?php
function donustur($karakter){
$tk=array("ç","ş","ö");
$ik=array("c","s","o");
$ykarakter= str_replace($tk,$ik,$karakter);
return $ykarakter;
}
if (isset($_POST['adi']) $ad = donustur($_POST['adi']);
if (isset($_POST['mesaj']) $mesaj =donustur($_POST['mesaj']);
echo "ad : ".$ad;
echo "ad : ".$mesaj;
?>
index.php
<?php
<form action="control.php" method="post">
adi : <input type="text" value="adi" size="10" /><br />
soyadı: <textarea cols="30" rows="30" wrap="virtual" maxlength="90" name="mesaj"></textarea><br />
<input type="submit" value="submit" />
</form>
?>

Related

What is the difference between POST and GET in HTML/PHP [duplicate]

This question already has answers here:
When should I use GET or POST method? What's the difference between them?
(15 answers)
What is the difference between POST and GET? [duplicate]
(7 answers)
Closed 9 years ago.
I am coding a PHP script, but I can't really seem to get it to work. I am testing out the basics, but I don't really understand what GET and POST means, what's the difference? All the definitions I've seen on the web make not much sense to me, what I've coded so far (but since I don't understand POST and GET, I don't know how to get it to work:
<form name="mail_sub" method="get">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['mail_sub']))
{
echo $_POST['theirname'];
}
?>
$_POST isn't working for you because you have the form method set to get.
<form name="mail_sub" method="post">
There's plenty of better info online as to the difference between post and get so I won't go into that but this will fix the issue for you.
Change your PHP as well.
if ( isset( $_POST['theirname'] ) ) {
echo $_POST['theirname'];
}
To answer the question:
GET and POST are one of the many request types in the standards of internet.
The difference is that GET can't post data, parameters will be appended to the url (url-parameters) which has it's limitations. POST does post parameters/data.
The standard is:
GET for getting data.
POST for creating data.
PUT for updating data.
DELETE for removing data.
replace
isset($_POST['mail_sub'])
by
isset($_POST['theirname'])
The main difference between GET and POST requests is that in GET requests all parameter are part of the url and the user sees the parameters. In POST requests the url is not modified and all form parameter are hidden from the user. If you have no file uploads or very long field parameter use GET. Use POST when going in production.
Post is like "sending" the data to the page without giving the user having any interaction with it, when Get shows the parameters in the URL and making it public for the user. Get is more often used on "unsecure" type of datatransactions like for example a searchform and POST is used when you want to make more secure things like a login form.
Using a Get will give you like
index.php?parameter=hello&parameter2=world
In your example you should use either POST in the method attribute in the formtag or $_GET in the php section
So either
<form name="mail_sub" method="post">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['theirname']))
{
echo $_POST['theirname'];
}
?>
or
<form name="mail_sub" method="get">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_GET['theirname']))
{
echo $_GET['theirname'];
}
?>
GET - If form method is GET then on submitting the form, Form will be submitted to
xyz.com/submit.php?theirname=john&theirpass=password // CHECK HERE - form elements are in URL so not so secure
and can be checked by PHP
as
if(isset($_GET['their']))
{
$name=$_GET['their']; //name will return John
}
POST - If form method is POST then on submitting the form, Form will be submitted to
xyz.com/submit.php // CHECK HERE form elements are not in URL
and can be checked by PHP
as
if(isset($_POST['their']))
{
$name=$_POST['their']; //name will return John
}
you used method as GET in form,so your code should be like this
<?php echo $_SERVER['PHP_SELF'];?>//the form and action will be same page
<form name="mail_sub" method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" value="Join!" style="width:200px">
</form>
<?php
echo $_GET['theirname'];
?>
Replace Your Code:
<form name="mail_sub" method="post">
Name: <input type="text" name="theirname"> <br />
Email: <input type="text" name="theirpass"> <br />
<input type="submit" name="submit" value="Join!" style="width:200px">
</form>
<?php
if (isset($_POST['submit']))
{
echo $_POST['theirname'];
}
?>

full email id passing in url

i am new to php .i am trying to develop a application with html and php.
i passed variable from form to url.all code given below
<FORM METHOD="get" ACTION="action1.php">
Type your first name: <INPUT TYPE="text" NAME="FirstName"> <br/>
Type your last name: <INPUT TYPE="text" NAME="LastName"> <br/>
Type your email:<INPUT TYPE="email" NAME="email">
<INPUT TYPE="submit" VALUE="Click and See">
</FORM>
the output url is http://example.com/action1.php?FirstName=jon&LastName=Kuri&email=xyz%40abc.com
but i want it to be http://example.com/action1.php?FirstName=jon&LastName=Kuri&email=xyz#abc.com
How to do it.
Thanks in advance for any suggestion.
update:
<?php
$email="xyz#gmail.com";
$decode=urldecode($email);
?>
<FORM METHOD="get" ACTION="action1.php">
Type your first name: <INPUT TYPE="text" NAME="FirstName"> <br/>
Type your last name: <INPUT TYPE="text" NAME="LastName"> <br/>
Type your email:<INPUT type="hidden" NAME="email" value="<?php $decode; ?>">
<INPUT TYPE="submit" VALUE="Click and See">
</FORM>
This also does not give me the required result.here i am pre defining email value.
Urls are encoded, that's why # becomes %40, it comes from the way data is sent from the form/ If you use get method, this is how it will be formated in the url.
But don't worry, what you get in php is correct and decoded.
You are simply seeing certain characters encoded to be URL safe (like #).
When you examine the data server-side it will look like the original data on the form. You don't have to decode anything, PHP does it for you automatically.
From the docs:
The superglobals $_GET and $_REQUEST are already decoded.
Try to echo $_GET['email'] in PHP and you'll see xyz#abc.com again.
Your code is incorrect as you are decoding an already decoded email.
<?php
$email="xyz#gmail.com";
$decode=urldecode($email);
// # should be encoded as %40
?>
should be:
<?php
$email="xyz%40gmail.com";
$decode=urldecode($email);
// returns "xyz#gmail.com"
?>
As stated in other answers, php will in most cases take care of this for you, however if you're trying to test your code with strings the above is necessary. Alternatively you could encode your email string too:
<?php
$email="xyz#gmail.com"; // no encoding here
$decode=urldecode(urlencode($email));
// returns "xyz#gmail.com"
?>

keep form value after submit it

I know there are lots others question like this and i found 1.000 answer on the web but none of those work with my code :(
So can someone help me with how to keep email value after submit?
<form name="login-registration" onSubmit="return validateForm()" method="post" action="" >
<label>Email</label>
<input type="email" name="emailinput" id="emailinput" value ="" />
<p id="emptyEmail" class="hidden">Email field is required</p>
<p id="invalidEmail" class="hidden">Email you insert is invalid!</p>
<label>Your password</label>
<input type="password" name="pswinput" id="pswinput" value=""/>
<p id="pswMinMax" class="hidden">Password should be from 4 to 8 caracters</p>
<p id="pswLettNum" class="hidden">Password should be letters and numbers. No special caracters are allow</p>
<label>Repeat password</label>
<input type="password" name="pswrepeatinput" id="pswrepeatinput" value="" onblur="isValidPswRep()"/>
<p id="pswR" class="hidden">Your passwords is different</p>
<input type="checkbox" id="policy" name="policy" value="policy" /> <label>I agree</label>
<p id="checkN" class="hidden">You must agree to our term</p>
<input type="submit" name="submit" value="Login" />
</form>
I try to put some code like:
<input type="email" name="emailinput" id="emailinput" value = "<?php echo htmlspecialchars($_GET['lastname']); ?>" />
But that php line is just displayed inside the field input.
try using $_POST['lastname'] instead of $_GET['lastname']
1)If i am not wrong,i don't see any field with name="lastname" in your code above.
2)Use $_POST because you are posting your form data with method="post".
Assuming that your file have .php as extension and php is installed on your server i would like you to notice that you have an error because you used a POST form while when you apply value to your input field you are trying to use $_GET Further more you did not assign lastnameto any input field, so use emailinput as you apply to this field name="emailinput". You should change htmlspecialchars($_GET['emailinput']); to be htmlspecialchars($_POST['emailinput']);
So your code would look like this
<input type="email" name="emailinput" id="emailinput" value = "<?php echo htmlspecialchars($_POST['emailinput']); ?>" />
This should print your variable inside your input field
There are at least 2 problems there.
The fact that the php is displayed inline suggests that either you have wrong file extension (like the comments suggested), either file is not included in your "know how to read by php" (for lack of a better way to say it, my English is not perfect) directory.
You echo a get when you sent a post (as other answer suggested)
Also... WHY DOES YOUR QUESTION HAVE JS TAG (sorry for caps, wanted to be sure it sticks out)?

How do I retrieve a PHP variable in my HTML code?

I call a PHP script from my HTML form submit.
Then I process the values retrieved from this HTML form in the PHP script and now I want to display these values in another HTML form that I am calling from this PHP script.
How do I retrieve this variable?
Note: I tried echo but I think I actually want to display this value in the form (HTML) and not break it again in a PHP tag.
I'm not sure what you mean by "not breaking it again with a PHP tag". HTML on its own cannot access PHP variables. This is because PHP is a server-side language and HTML is a client side language. But here is the simplest way to print php variables retrieved from a form.
<form>
<p>Your name: <?php echo $_POST['name'] ?></p>
</form>
This is assuming that there was a form that submitted a field called 'name' using POST.
You can process the name in a php script at the top of the file and then simply echo it when you're printing the html. This way, you won't have too much php code mixed in with the HTML (which makes it look cleaner).
Once you got the values in the PHP script, are you calling a new script? If so, you might wanna save the values in $_SESSION["varible_name"]. If not, you just have to echo it.
It depends on how you are accessing your form data, either through $_POST or through $_GET. For simplicity, I'll assume your using $_GET and modify this example for more clarity.
So lets say you have a form hosted on welcome.php:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Now the results will be returned back to the same page, so you want to modify the page to say:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo $_GET["fname"]; ?>"/>
Age: <input type="text" name="age" value="<?php echo $_GET["age"]; ?>" />
<input type="submit" />
</form>
Though you'll notice that we're using the same page, and we can only have one version of the page, so we want to render if upon the condition that our variable has been set.
if (isset($_GET["fname"])){
//code to print second form
}
else{
//code to print first form
}
Or, in another way (using the ternary operator):
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo ((isset($_GET["fname"]))?$_GET["fname"]:""); ?>"/>
Age: <input type="text" name="age" value="<?php echo ((isset($_GET["age"]))?$_GET["age"]:""); ?>" />
<input type="submit" />
</form>

Passing URL variable via HTML form submission using either PHP or JavaScript

I need to simply pass a form variable into a URL variable. I suspect that it's something easy to do but I'm having a hard time finding clear steps (that aren't tons of code) online anywhere.
Here's my current form code
<form id="zip_search" method="post" action="dealers.php">
<label for="zipfield">Find a Dealer</label>
<input name="tZip" type="text" id="zipfield" value="ZIP CODE" onblur="if(this.value=='') this.value='ZIP CODE';" onfocus="if(this.value=='ZIP CODE') this.value='';" />
<input type="image" value="Submit" class="submitbutton" src="/images/submit_button.gif" />
</form>
And all I need is for it to send the browser to something like this:
http://www.mydomain.com/dealers.php?zip=55118
Thank you in advance for any help.
Update to question
Thanks to Drew and Anton's responses here's an update. Changing the input name attribute to match the URL var name (tZip to zip) along with changing POST to GET did the trick but for some reason it's adding two additional URL variables as well (&x=0&y=0). I'm guessing this is something incorrect with my PHP code as I'm not a PHP wizard by any stretch. Here's all the code:
PHP Function
<?php
function processForm() {
$zipCode = $_GET['zip'];
$url = "dealers.php?zip=" . $zipCode;
header("Location: $url");
exit;
}
?>
Form
<form id="zip_search" method="get" action="dealers.php">
<label for="zipfield">Find a Dealer</label>
<input name="zip" type="text" id="zipfield" value="ZIP CODE" onblur="if(this.value=='') this.value='ZIP CODE';" onfocus="if(this.value=='ZIP CODE') this.value='';" />
<input type="image" value="Submit" class="submitbutton" src="/images/submit_button.gif" />
</form>
URL Output Example
http://www.domain.com/dealers.php?zip=12345&x=0&y=0
Additional Related Question
How is this working if processForm() is only defined but not called anywhere else. It seems to me that the processForm() function should be in the action attribute in the opening form element. Any insight? Thanks in advance.
Change the form method to "get"
You will need to change the form method from POST to GET and also rename the text input from tZip to zip otherwise your URL will look like this:
http://www.mydomain.com/dealers.php?tZip=55118
instead of
http://www.mydomain.com/dealers.php?zip=55118

Categories