Ok so Im trying to use radio buttons in my PHP code. I wrote a Madlib in PHP and I want to write in a radio button that when a user selects it, it will change the story etc. Happy ending or sad ending here the code I have so far. Any help would be appreciated.
<html>
<head>
<title>James Nygaard's Mablib</title>
</head>
<body bgcolor="<?php echo $bg; ?>" text="<?php echo $fg; ?>">
<h1 style="font-family:cursive;">Create your Madlib below:</h1>
<form action="" method="post">
Enter the name of a boy:
<input name="noun1" type="text" value="<?= $_POST['noun1'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj1" type="text" value="<?= $_POST['adj1'] ?>" /><br />
Enter the name of a man:
<input name="noun2" type="text" value="<?= $_POST['noun2'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj2" type="text" value="<?= $_POST['adj2'] ?>" /><br />
Enter the name of a woman:
<input name="noun3" type="text" value="<?= $_POST['noun3'] ?>" /><br />
Enter your favorite animal:
<input name="noun4" type="text" value="<?= $_POST['noun4'] ?>" /><br />
Enter a name:
<input name="noun5" type="text" value="<?= $_POST['noun5'] ?>" /><br />
Enter the name of your favorite city:
<input name="noun6" type="text" value="<?= $_POST['noun6'] ?>" /><br />
Enter a feeling that ends in "ness":
<input name="adj3" type="text" value="<?= $_POST['adj3'] ?>" /><br />
Enter a Verb that ends in "ing":
<input name="verb2" type="text" value="<?= $_POST['verb2'] ?>" /><br />
Enter the name of a boy:
<input name="noun7" type="text" value="<?= $_POST['noun7'] ?>" /><br />
Enter the name of a girl:
<input name="noun8" type="text" value="<?= $_POST['noun8'] ?>" /><br />
<input type="submit" value="Click here or press enter to see your MadLib" /><br />
</form>
<div style="color: #2F4F4F; font-family: cursive;">
<?php
$bg = "8FBC8F";
$fg = "2F4F4F";
if(isset($_POST['noun1'])) {
echo "<h1> The adventures of {$_POST['noun1']}. </h1>";
echo "This is where i will put the story. I already have the story but the code is really long so I left it out of this question.";
echo "<br> The end.";
}
?>
</div>
</body>
</html>
It's not especially clear in this small space, but I tried to follow your code style.
Select an option:
<label>
<input name="some_option" type="radio" value="option_1" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_1') echo 'checked="checked" '; ?>/> Option 1
</label>
<label>
<input name="some_option" type="radio" value="option_2" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_2') echo 'checked="checked" '; ?>/> Option 2
</label>
If you run that code you should see what's going on.
Related
in PHP CodeIgniter application there are several pages with input text boxes and text areas.
How to implement the session save in php i.e. whnever the user closes the tab,
the entered data should remain saved until the user logs out of the application.
or is there any plugin available to achieve the same ?
my code for the same :-
<form accept-charset="UTF-8" method="post" id="myform" action="/saveform">
Name: <input name="name" type="text" value="" /><br />
Address: <textarea name="address"></textarea><br />
Gender: <select name="gender">
<option value="M">Male</option>
<option value="F">Female</option>
</select><br />
Interests:<br />
1: <input name="interests[]" type="text" value="" /><br />
2: <input name="interests[]" type="text" value="" /><br />
3: <input name="interests[]" type="text" value="" /><br />
4: <input name="interests[]" type="text" value="" /><br />
5: <input name="interests[]" type="text" value="" /><br />
Sign up for our newsletter: <input name="signup" type="checkbox" value="1" /><br />
<input name="submit" type="submit" value="Submit" />
</form>
You can save the form data whenever the tab or page is closed, using AJAX you can save all data in the session and then in form you can add modify like this:
<form accept-charset="UTF-8" method="post" id="myform" action="/saveform">
Name: <input name="name" type="text" value="<?php echo isset($_SESSION['user']['name']) ? $_SESSION['user']['name'] :"";?>" /><br />
Address: <textarea name="address"><?php echo isset($_SESSION['user']['address']) ? $_SESSION['user']['address'] :"";?></textarea><br />
Gender: <select name="gender">
<option value="M" <?php echo $_SESSION['user']['gender']=='M' ? "selected" :"";?>>Male</option>
<option value="F" <?php echo $_SESSION['user']['gender']=='F' ? "selected" :"";?>>Female</option>
</select><br />
Interests:<br />
1: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_0']) ? $_SESSION['user']['interests_0'] :"";?>" /><br />
2: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_1']) ? $_SESSION['user']['interests_1'] :"";?>" /><br />
3: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_2']) ? $_SESSION['user']['interests_2'] :"";?>" /><br />
4: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_3']) ? $_SESSION['user']['interests_3'] :"";?>" /><br />
5: <input name="interests[]" type="text" value="<?php echo isset($_SESSION['user']['interests_4']) ? $_SESSION['user']['interests_4'] :"";?>" /><br />
Sign up for our newsletter: <input name="signup" type="checkbox" value="1" <?php echo $_SESSION['user']['signup']=='1' ? "checked" :"";?> /><br />
<input name="submit" type="submit" value="Submit" />
</form>
Hope this helps !!
I wish every time I reload or refresh the page. The disabled textbox still disabled. Because my purpose is to modify the textbox once time and then it will be disabled forever. Is this possible?
Below is my code
<tr>
<td><?php echo $lang_txt['leader_id'][$lang]; ?></td>
<td>:</td>
<td><?php if($_POST['txtLeaderID'] == ""){?><input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php }
else
{
?>
<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php
}
?>
</td>
</tr>
Your variant will be worked with
if(!$_POST['txtLeaderID']){echo('<input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')} else {echo('<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')}
For example:
<?php
if (!$_POST['username']){
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>
But it didn't work, if we open it in a new tab. You must save this propery anywhere (session, cookie, sql)and check the stored values. it will be work in a new tab.
For example with session:
<?php
session_start();
if (!isset($_SESSION['data'])) {
$_SESSION['data'] = true;
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>
I´m trying to show in a '<pre></pre>' the values that are passed in the form.
But Its not working and give my some errors:
-> Notice: Undefined index: email in
-> Notice: Undefined index: pass
-> Notice: Undefined index: remember
I already tried put isset like this:
$f['email'] = mysql_real_escape_string(isset($_POST['email']));
$f['pass'] = mysql_real_escape_string(isset($_POST['pass']));
$f['save'] = mysql_real_escape_string(isset($_POST['remember']));
And this way above I dont have erros but the data that I write in the form inputs dont show in my
echo '<pre class="debug">';
print_r($f);
echo '</pre>';
Can you see something that Im doing wrong?
My code:
<?php
if(isset($_POST['sendLogin']))
{
$f['email'] = mysql_real_escape_string($_POST['email']);
$f['pass'] = mysql_real_escape_string($_POST['pass']);
$f['save'] = mysql_real_escape_string($_POST['remember']);
echo '<pre class="debug">';
print_r($f);
echo '</pre>';
}
?>
<?php
if(!isset($_GET['remember']))
{
?>
<form name="login" action="" method="post">
<label>
<span>Email:</span>
<input type="text" class="radius" name="<?php if($f['email']) echo $f['email']; ?>" />
</label>
<label>
<span>Password:</span>
<input type="password" class="radius" name="<?php if($f['pass']) echo $f['pass']; ?>" />
</label>
<input type="submit" value="Login" name="sendLogin" class="btn" />
<div class="remember">
<input type="checkbox" name="remember" value="1"
<?php if(isset($f['save'])) echo 'checked="checked"' ?>
/>
I think that you have made mistake with name attribute. You should have static name="email" and name="pass" in the inputs. While your current name attribute change to value:
<input type="text" class="radius" name="email" value="<?php if($f['email']) echo $f['email']; ?>" />
<input type="password" class="radius" name="pass" value="<?php if($f['pass']) echo $f['pass']; ?>" />
Your inputs must have the name attribute in order to be accessible in the PHP
<form name="login" action="" method="post">
<label>
<span>Email:</span>
<input type="text" class="radius" value="<?php if($f['email']) echo $f['email']; ?>" name="email" />
</label>
<label>
<span>Password:</span>
<input type="password" class="radius" value="<?php if($f['pass']) echo $f['pass']; ?>" name="password" />
</label>
<input type="submit" value="Login" name="sendLogin" class="btn" />
<div class="remember">
<input type="checkbox" name="remember" value="1"
<?php if(isset($f['save'])) echo 'checked="checked"' ?>
/>
In <input type="text"> as well as in <input type="password"> block, provide a meaningful value for the name attribute.
Here in your code provide name="email" and for the next one name="pass".
Also to display the email/pass values after the form submission, give it in value attribute.
Right now you wrongly given inside the name attribute.
value="<?php if($f['email']) echo $f['email']; ?>" and
value="<?php if($f['pass']) echo $f['pass']; ?>"
I am new to php and mysql. i've used radio buttons to save the type of my account records i can insert the data successfully but when i try to get that data (whether type S is selected or type T is selected) from sql to update it further i cant do that. i dont know how to get the data of radio buttons from my sql.. please help
<form method="post" action="users-edit-action.php">
<input type="hidden" value="<?php echo $accountid; ?>" name="id" />
<label>Email/Username:</label><input type="text" name="email" value="<?php echo $email; ?>" /><br /><br />
<label>Password:</label><input type="password" name="password" value="<?php echo $password;?>" /><br /><br />
<label>First Name:</label><input type="text" name="firstname" value="<?php echo $firstname; ?>" /><br /><br />
<label>Last Name:</label><input type="text" name="lastname" value="<?php echo $lastname; ?>" /><br /><br />
<label>Type:</label><br />
<input type="radio" name="type" value="<?php echo $type; ?>" /> Student<br />
<input type="radio" name="type" value="<?php echo $type; ?>" /> Teacher<br />
<input type="submit" value="Edit" />
</form>
this is how i get the value from database.. im sure iam doing wrong, please help me solve this..
You have to check which type you got from the database and according to that add this attribute to the input element: checked="checked". So the whole selected tag would look like this:
<input name="type" type="radio" checked="checked" value="the one PHP returned" />
And the other input tag would also be there, but without the checked attribute.
You just need to do the PHP check on what element to put the attribute.
The whole part:
<input type="radio" name="type" value="S" <?php if ($type == 'S') echo 'checked="checked"'; ?>" /> Student<br />
<input type="radio" name="type" value="T" <?php if ($type == 'T') echo 'checked="checked"'; ?>" /> Teacher<br />
The value of your radio-buttons is fixed and does not depend on the value in the database, only whether it is checked or not.
It should be something like:
<input type="radio" name="type" value="S" <?php echo ($type == 'S') ? 'checked' : ''; ?> /> Student<br />
<input type="radio" name="type" value="T" <?php echo ($type == 'T') ? 'checked' : ''; ?> /> Teacher<br />
I can't seem to figure out how to make this script write data to the section of the file. Here is an excerpt of the code that applies:
$file = $url.'.php';
if (!$file_handle = fopen($file,"a+"))
{
echo $lang['cannot_open_file'];
}
if (!fwrite($file_handle,stripslashes(html_entity_decode($data))))
{
echo $lang['file_is_not_writable'];
}
fclose($file_handle);
echo ($lang['success']);
echo ('<meta http-equiv="Refresh" content="4;url='.$url.'.php" />');
}
}
?>
<form action="<?php echo $url;?>.php" method="post">
<?php echo $lang['name']?> <span style="font-weight:100;">(<?php echo $lang['required']?>)</span>:<br />
<input class="textfield" type="text" name="name" value="" /><br />
<?php echo $lang['website']?> <span style="font-weight:100;">(<?php echo $lang['without_http'];?>)</span>:<br />
<input class="textfield" type="text" name="site" value="" /><br />
<?php echo $lang['message']?>:<br />
<textarea class="textarea" rows="2" cols="25" name="message"></textarea><br />
<img src="captcha-image.php" alt="Image verification" /> <br />
<?php echo $lang['value_from_the_image'];?>:<br />
<input class="textfield" type="text" name="img_ver" value="" /><br />
<input type="reset" name="reset" value="<?php echo $lang['reset'];?>" />
<input type="submit" name="send" value="<?php echo $lang['send'];?>" />
</form>
<hr>
<h3>Comments:</h3>
<!--comments -->
<?php include('../templates/footer.php'); ?>
Would anyone mind helping me out on this one? It would be much appreciated.
Use file get contens to retrieve the contens first.
Then use preg_replace to find where or what you want to be replaced.
Then rewrite the file with the new content.