Not validating one of the search fields - php

I want to check if either of my search fields are empty, and if they are i write out a failure message. For some reason the following code doesn't work when I try to submit empty values in both search fields, or leaving the q2 search field empty. It only writes out the failure message when I only insert a value into the first field. Hope you guys can help.
<div id="searchField">
<form name="searchField" action="" method="get">
<input type="search" name="q" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="search" name="q2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>
<input type="hidden" id="v" name="v" value="2" />
<input type="hidden" id="b" name="b" value="" />
<button type="submit" id="loop" style="border:0px; background:transparent; vertical-align:middle; margin-left:-85px; margin-top:-3px;">
<img src="images/loop.png" width="75" height="75" alt="submit" />
</button>
</form>
</div>
<div id="theData">
<?
if(str_replace(" ", "",$_GET['q'])!="" && str_replace(" ","",$_GET['q2'])!=""){
$qContent = $_GET['q'];
$succes = mysql_query("SELECT*FROM film WHERE name='".$qContent."'");
include("searchMaster.php");
if(mysql_num_rows($succes)) {
while($o = mysql_fetch_array($succes)){
echo $o['name'];
if($o['trailer']){
echo '<div id="ytplayerWrap">';
echo '<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/'.$o['trailer'].'?http://amnestic.ueuo.com"
frameborder="0"/>';
echo '</div>';
}else {
echo '<span style="font-size:20px;">';
echo '<br /><br /> No trailer was found for <em>'.$_GET['q'].'</em> :(';
echo '</span>';
}
}
}else{
echo 'You did the fuckup :(';
}
}else{
echo 'You did the fuckup :(';
}
?>

The name and id of your input fields must be same
Like this
<input type="text" name="submitQuery" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="text" name="submitQuery2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>

Related

PHP - Show different html if variable is zero or not

Been searching around here but where only able to find to show/hide if the variable isset or not.
I want to show two different infos depending if the variable is zero or not.
If variable is zero the text input and submit button should be visible.
If variable is not zero the facebook variable should be echoed and the reset button showed.
What am i doing wrong?
Heres my code ive been trying:
<?php
$sql2 = $db->query( "SELECT facebook FROM ".$db->prefix."users WHERE id = ".$id);
while ( $row = mysql_fetch_assoc ( $sql2 ) ) {
if (empty($row)) {
?>
<input type="hidden" name="form_sent" value="1" />
<input type="text" name="form[facebook]" value="<?php echo pun_htmlspecialchars($user['facebook']) ?>" size="40" maxlength="50" />
<input type="submit" name="update" value="<?php echo $lang_common['Submit'] ?>" />
<?php
} else {
?>
<?php echo pun_htmlspecialchars($user['facebook']) ?>
<a href="reset.php">
<input type="button" value="Request reset" />
</a>
<?php
}
}
?>
FIX EDIT
I just did it this way and it works fine.
<?php
if($user['facebook'] == '0'){
echo '<input type="button" value="Submit" />';} ?>
<?php
if($user["facebook"] > 0){
echo '<input type="button" value="Request reset" />'; }?>
Now why cant i add this in the echo?
<input type="text" name="form[facebook]" value="<?php echo
pun_htmlspecialchars($user['facebook']) ?>" size="40"
maxlength="50" />
I guess its because php echoes php?
Is there a workaround for this?
try this
<?php
$sql2 = $db->query( "SELECT facebook FROM ".$db->prefix."users WHERE id =
".$id);
while ( $row = mysql_fetch_assoc ( $sql2 ) ) {
if ( $row['facebook'] == 0 ) {
?>
<input type="hidden" name="form_sent" value="1" />
<input type="text" name="form[facebook]" value="<?php echo
pun_htmlspecialchars($user['facebook']) ?>" size="40"
maxlength="50" />
<input type="submit" name="update" value="<?php echo
$lang_common['Submit'] ?>" />
<?php
} else {
echo pun_htmlspecialchars($user['facebook'])
?>
<a href="reset.php">
<input type="button" value="Request reset" />
</a>
<?php
}
}
?>
I just did it this way and it works fine.
<?php
if($user['facebook'] == '0'){
echo '<input type="button" value="Submit" />';} ?>
<?php
if($user["facebook"] > 0){
echo '<input type="button" value="Request reset" />'; }?>
Now why cant i add this in the echo?
<input type="text" name="form[facebook]" value="<?php echo
pun_htmlspecialchars($user['facebook']) ?>" size="40"
maxlength="50" />
I guess its because php echoes php?
Is there a workaround for this?

PHP radio button

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.

Multiple forms on same page - If empty

I'm currently in the process of coding a MySQL search page and I'm unable to work out how to make it so that if there's no data put in one form it'll do nothing, and if there's data in the other form it'll search the database for that value.
<?PHP
echo '<h3 class="hp-1">Kick Logs</h3><div class="wrapper">
<div class="logcol-1"><form name="form1" id="mainForm" method="post"enctype="multipart/form-data" action="' . $_SERVER['REQUEST_URI'] . '">
<input name="name" type="text" id="name" placeholder="Players Name">
</form>';
echo '<form name="form2" id="mainForm" method="post"enctype="multipart/form-data" action="' . $_SERVER['REQUEST_URI'] . '"><input name="reason" type="text" id="reason" placeholder="Kick Reason"></form>';
$name = mysql_real_escape_string($name);
$reason = mysql_real_escape_string($reason);
$kicklogname = mysql_query("SELECT * FROM `log1` WHERE `user` LIKE '%$name%'") or die(mysql_error());
$kicklogreason = mysql_query("SELECT * FROM `log1` WHERE `user` LIKE '%$reason%'") or die(mysql_error());
if($name == ""){
echo "You must enter a name to search"; }
else {
echo '<table width="700" border="0">
<tr class="listheader">
<td width="100" bgcolor="#afe6ff">Username</td>
<td width="220" bgcolor="#afe6ff">Reason</td>
</tr>';
while($row = mysql_fetch_array($kicklogname))
{
echo '<tr><td bgcolor="#daf4ff" class="contentleft">';
echo $row['user'];
echo '</td><td bgcolor="#eefaff" class="contentright">';
echo $row['reason'];
echo '</td></tr>';
}
echo '</table></div></div>';
}
?>
Update
Sorry, re-read the question. To check multiple forms on the same page you need to add a submit button to each form and give it a name:
<?php
if (!empty($_POST) && isset($_POST['reasonsubmit'])) {
echo 'Submitted reason form';
}
if (!empty($_POST) && isset($_POST['namesubmit'])) {
echo 'Submitted name form';
}
?>
<form action="reason.php" method="post">
<input type="text" name="reason" id="reason" />
<input type="submit" name="reasonsubmit" />
</form>
<form action="name.php" method="post">
<input type="text" name="name" id="name" />
<input type="submit" name="namesubmit" />
</form>
Alternatively, if you didn't want to give the submit button a name you can just add a hidden field to each form and you will get the same behavior.
<form action="reason.php" method="post">
<input type="hidden" name="reasonsubmit" id="reasonsubmit" />
<input type="text" name="reason" id="reason" />
<input type="submit" />
</form>
<form action="name.php" method="post">
<input type="hidden" name="namesubmit" id="namesubmit" />
<input type="text" name="name" id="name" />
<input type="submit" />
</form>

Keep form data inside the field after submission using php

I am using below code for a html form.(it has two forms) I am able to keep the textarea field after the first and second form submission. but issue I am facing here is the dropdown menu selection.
Code:
<html>
<body>
<div class="emcsaninfo-symcli-main">
<form id="form1" name="form1" action=" " method="post" >
<div class="input">Your Name</div>
<div class="response"><span><input class="textbox" id="myname" name="myname" type="text" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>" /></span> </div>
<div class="input">Your Place</div>
<div class="response"><span><input class="textbox" id="myplace" name="myplace" type="text" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" /></span> </div>
<div class="input-quest">Graduation Status</div>
<div class="input-resp"><select id="graduation" name="graduation" OnChange="CMT();"><option class="dropdown-options">Graduate</option><option class="dropdown-options">Non Graduate</option></select></div>
<div class="submit">
<input id="first_submit" type="submit" name="first_submit" value="first_submit" />
</div>
</form>
<?php
if(!empty($_POST['myname']) && !empty($_POST['myplace']) || !empty($_POST['output_textarea'] ) )
{
$myname = $_POST['myname'];
$myplace = $_POST['myplace'];
$graduation = $_POST['graduation'];
?>
<form id="form2" name="form2" action=" " method="post" >
<textarea onclick="this.select()" name="output_textarea" id="output_textarea" cols="100" rows="25" readonly value="<?php if(isset($_POST['output_textarea'])) { echo htmlentities ($_POST['output_textarea']); }?>">
<?php
echo "My name is $myname and I am from $myplace, and I am $graduation";
?>
</textarea>
<input id="submit1" type="submit" name="name_field" value="submit1" />
<input id="submit2" type="submit" name="place_field" value="submit2" />
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
</form>
<?php
function name()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['name_field']))
{
name();
}
function place()
{
echo $_POST["output_textarea"];
}
if(isset($_POST['place_field']))
{
place();
}
}
?>
</div>
</html>
</body>
For example if I put name = John, place : UK and selecting graduation status as graduate, it will will give me first form output as in my output textarea
My name is John and I am from UK, and I am Graduate
I have two seperate submit button for second form, using that I am doing some other function with help of the output textarea
If I press any of the second button,I m able to keep entries my name and place area, but it not keeping the dropdown selection. so it will only display like after submitting submit1 or submit2
My name is John and I am from UK, and I am
Here,
How can I keep the the dropdown selection also with the output text area
Will I able to show only the output_textarea content after second form submission without keeping the first form data ?
PHP FIDDLE
You have an error in logic in the hidden input for the "graduate" element.
This is what you have at lines 53-55. Line 55 doesn't have an echo instead it has an $graduation = $_POST['graduation']; which won't help you:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { $graduation = $_POST['graduation']; }?>" />
instead of that, this code should work:
<input id="myname_hidden" name="myname" type="hidden" value="<?php if(isset($_POST['myname'])) { echo htmlentities ($_POST['myname']); }?>"/>
<input id="myplace_hidden" name="myplace" type="hidden" value="<?php if(isset($_POST['myplace'])) { echo htmlentities ($_POST['myplace']); }?>" />
<input id="graduation_hidden" name="graduation" type="hidden" value="<?php if(isset($_POST['graduation'])) { echo htmlentities($_POST['graduation']); }?>" />

How To fopen and write to a specified section on a file

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.

Categories