We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,089 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP form Save text - Noob question

Greetings all,
I have a noob php question i need answering if possible..
I simply want to save what ever is typed into the form field for when the page is reloaded to show again.
I know it needs a new session started but im confused alot about what needs changing.
Here is my php of code:

<?php

include("config.php");
if(isset($_POST['submit'])) {
$word=$_POST['q'];
if($word=="") { $error="<font color='red'>Please enter a keyword</font>"; }
if($error=="") {
$sql=mysql_query("SELECT * FROM slogans ORDER BY RAND() LIMIT 1");
$r=mysql_fetch_array($sql);
echo '<font size="6">'; 
$slogan=$r['slogan'];
$slogan=str_replace("[keyword]",$word,$slogan);

echo $slogan;
echo '</font>';
}
}
echo $error; ?>

And my Form:

<form method="post">

<input type="text" name="q" VALUE="Slogan Maker" size="35" maxlength="255"  style="height:38px;font-size:18px;" onFocus="this.value=''"/>
<input type="submit" name="submit" value="Make Slogan!"> 

</form> 

Thanks alot! plz help

2
Contributors
5
Replies
12 Hours
Discussion Span
8 Months Ago
Last Updated
6
Views
jemsz
Newbie Poster
4 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Ok, you need to use a session. Place session_start() at the top of the file and use:

<?php
session_start();
if($_POST){
     $_SESSION['q'] = $_POST['q'];
     $q = $_SESSION['q'];
}     
?>

<input type="text" name="q" VALUE="Slogan Maker" size="35" maxlength="255"  style="height:38px;font-size:18px;" value="<?php echo $q;?>" />
<input type="submit" name="submit" value="Make Slogan!">      

Do you need to take off the onfocus attibute?
You could unset the $_SESSION['q'] after the $q= ... line if you don't want the text to be propogated beyond the post action, e.g. page refresh.

diafol
Keep Smiling
Moderator
10,647 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,509
Skill Endorsements: 57

Hey thanksa lot for the reply! I have tried the ubove code and it seems it still doesnt save the form data when the page is reloaded. Does the PHP form u just sent go into the same area as my php form? or in diffrent php brackets all together.. Sorry i am very noob at php lol

jemsz
Newbie Poster
4 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Should go to the same page - sorry forgot the form tags! Doh!

<form method="post">
<input type="text" name="q" VALUE="Slogan Maker" size="35" maxlength="255"  style="height:38px;font-size:18px;" onFocus="this.value=''"/>
<input type="submit" name="submit" value="Make Slogan!"> 
</form>
diafol
Keep Smiling
Moderator
10,647 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,509
Skill Endorsements: 57

Thanks alot Diafol for the reply, unfortunetly it has still not worked..

value="<?php echo $q;?>

From ur original PHP was missing in the new one> does this effect it?
Thanks again

jemsz
Newbie Poster
4 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Oh gawd, doh! Here you go, hope I've got it right this time:

<?php
session_start();
if($_POST){
     $_SESSION['q'] = $_POST['q'];
     $q = $_SESSION['q'];
}     
?>
<form method="post">
<input type="text" name="q" VALUE="Slogan Maker" size="35" maxlength="255"  style="height:38px;font-size:18px;" value="<?php echo $q;?> />
<input type="submit" name="submit" value="Make Slogan!"> 
</form>

Just a note, if you send the form to itself (same page) a refresh following a form submit will resend the data. As a rule forms are sent to different pages/files for processing and then redirected back to the form if req'd. The session solution works equally well for this, although you need to spread the code over two pages/files.

diafol
Keep Smiling
Moderator
10,647 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,509
Skill Endorsements: 57

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0745 seconds using 2.73MB