Html form to text file - php

On my website i got a form looks like this on index.html:
<form id="demo" action='submit.php' method='post' enctype='text/plain'>
link: <input type='text' name='web'></br>
<input type='submit' value='submit'>
</form>
And then in the submit.php i got:
<?php
$fp=fopen('sub.txt','a');
fwrite($fp,addslashes($web));
fclose($fp);
header('location: thanks.html');
exit();
?>
But when i press submit the result in sub.php is beeing
/n
But it should be
example.com/n
What is wrong in the php code.
I want the link submited in the form printed in the file sub.txt and then the person be redirected to a thank you page.

You missing value attribute as well as you need $_POST to get the value inside sub.php
<form id="demo" action='submit.php' method='post' enctype='text/plain'>
link: <input type='text' name='web' value="" /></br>
<input type='submit' value='submit' />
</form>
<?php
$fp=fopen('sub.txt','a');
fwrite($fp,addslashes($_POST['web']). "\r\n");
fclose($fp);
header('location: thanks.html');
exit();
?>

Related

Saving info while submitting page with GET in PHP

<form method='get' action='y.php'>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['btnSave'])) {
$name=isset(($_GET['txtName'])?isset($_GET['txtName']:'');
//then Logic of insert goes here
}
}
?>
so before moving to y.php the record must be saved.
but I cant get the $name value, as action given to y.php.
How can I get $name which contain value in text box.
if you change the action to this (same/current) page record is going to database without any flaw or error.
try using post method instead and change your code accordingly, try this:
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit' name='submit'/>
</div>
</form>
<?php
if (isset($_POST['submit'])) {
$name=$_POST['txtName'];
//then Logic of insert goes here
//redirect to y.php with name value
echo "<script>window.open('y.php?user=$name','_self')</script>";
}
?>
Then use $nme = $_GET['user']; to get the value of $name in y.php
Try this code,
<form method='get' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['txtName'])) {
$name=$_GET['txtName'];
//then Logic of insert goes here
}
}
?>
I suggest you use method post. You can code like this
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if(isset($_POST['txtName'])){
$name =$_POST['txtName'];
echo $name;
}
?>
you can use the session if you want make the value use in another file.
I hope that can solve your problem

PHP Like Button Problems

I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.

Submit buttons not working while using type image

This is a simple demonstration of my code :
if(isset($_POST['login'])) {
do this
}
<form method='POST' action="index.php">
Username : <input type='text' name='username'/>
Password : <input type='password' name='password' />
<input type='image' src='images/login.png' name='login' value='login'/>
</form>
The problem is that , when i use type='image' as a submit button, in Firefox and IE nothing happends. But if i use type='submit' everything works fine..
Well i dont want to display the button as a submit one, but as an image, so what im doing wrong ?
Simply change your if to look for any other field in the POST. Image type button wont send that value.
Change:
if(isset($_POST['login'])) {
To
if(isset($_POST['username'])) {
If you want to check login, you need to check for login_x or login_y values instead of login, which is not created when button is an image.
<?php
if(isset($_POST['login_x'])) {
echo "Here I m";
}
?>
<form method='POST' action="index.php">
Username : <input type='text' name='username'/>
Password : <input type='password' name='password' />
<input type='image' src='images/login.png' name='login' value='login'/>
</form>
Sorry, I explained in a comment the type=image creating _x and _y, but I forgot the params login does not exists in that case

Multiple submit on form php

On my form I have 3 submit,I want to know the best way to handle these buttons because the default action is the same form:
<form method="POST" action="file_where_form_is.php">
Name:<input type='text' name='name'>
<input type='submit' name='add' value='Add Names' ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit'name='Preview'value='Preview'ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit' name='submit'value='Submit' ONCLICK='window.location.href="send.php">
</form>
This not seems to work completely,just the 2 first work and for the last submit does nothing(with name='submit')
Any suggestions?
I finally found in header part add something like this:
if(isset($_POST['submit'])){
header("location: somefile.php");
}
elseif(isset($_POST['Preview'])){
header("location: anotherfile.php");
}
elseif(isset($_POST['add'])){
header("location: anotheronefile.php");
}

I cant transfer html data with form above 3 pages

I have 3 pages.
a1.php : add record
a2.php : do sth
a3.php : record them database
But problem is occured, transfering "html data" from a2.php to a3.php.
EXAMPLE: a record which I try to record to a1.php is that :
<form name="form1" type="post" action="sayfa_1.php">
<input type='submit' value=' gidiyoruz' >
<input type='submit' value=' gidiyoruz' >
</form>
a1.php --> a2.php --> a3. php
On a2.php, there i no problem. But then. On a3.php when I show coming data, I SEE TWO BUTTON, NO HTML CODES which is above.
pages is below.
thanks.
a1.php
<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">
//<![CDATA[
bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
//]]>
</script>
<form name="form1" type="post" action="a2.php">
<textarea rows="20" cols="90" id="anlam" name="anlam" style="overflow:auto;" > </textarea>
<input type="submit" />
</form>
a2.php
<?php
$anlam=$_GET["anlam"];
echo $anlam;
?>
<?php echo "<form name='fm' id='fm' action='a3.php' method='get'>"; ?>
<textarea rows="2" cols="50" name="anlam" style="visibility:hidden;" /> <?php echo $anlam; ?> </textarea>
<input type="submit" />
</form>
a3.php
<?php
$anlam=$_GET["anlam"];
echo nl2br($anlam);
Pace images are above. error: a3.php (instead code, there are two button)
so you want the page to send your data from one to another when you click submit button!!
Use if condition with isset.
if(isset($_POST['name_of_submit_button']))
{
// your code of next page
}
n similarly continue with another page by giving another name to you submit button and applying the same condition!!
Try using
method="post"
for your form. You could also just use an
<input type="hidden" name="anlam" value="<?php echo $anlam; ?>"/>
to transport your variable content to the last page. Are you sure, that the hidden textarea/input contains the correct value? Check the source code in your browser for this step.

Categories