How to stop print echo result in output without function executed - php

It might be a silly question but,
I created a single PHP to do some checks in my site and at the final result
I used echo to print it or print "nothing found" in case function failed
but the issue is, the PHP file keep printing the "nothing found" even if i didn't click the "submit" button
if (function failed) {
echo "not found";
} else {
echo "do stuff here";
}
the php file keep printing "not found"
I tried ob_clean() ob_end_flush() ob_end_clean() but it didn't work
Full code:
<!DOCTYPE html>
<html>
<head>
<title>Check IMDB</title>
</head>
<body>
<div class="container">
<form method="POST" action="">
<input type="submit" value=" Check IMDB ">
<div>
<textarea style="width: 50%; margin-top:7px; height: 150px;" id="urlbox" name="urlbox" placeholder="Add imdb links here (one per line)"></textarea>
</div>
</form>
</div>
<?php
$imdblink = "";
$imdblink = $_POST['urlbox'];
?>
<?php
set_time_limit(0);
require(dirname(__FILE__) . '/wp-load.php');
global $wpdb;
echo "<br>";
if (!preg_match_all('~tt\d{7,8}~', $imdblink, $ttids)) {
echo "no ttids found";
} else {
$resultSet = $wpdb->get_results("SELECT meta_value FROM wp_postmeta WHERE meta_key = 'imdb' AND meta_value IN ('" . implode("','", $ttids[0]) . "');");
$foundInDatabase = array_column($resultSet, "meta_value");
foreach ($ttids[0] as $index => $ttid) {
if (in_array($ttid, $foundInDatabase)) {
$ttid = '<a style="color:blue;" href="https://www.imdb.com/title/'.$ttid.'/">'.$ttid.'</a>';
}
echo "<div>" , ($index + 1) , "- {$ttid}</div>\n";
}
}
?>
</body>
</html>

You should add a name attribute to your submit button and check if the button was pressed.
In your case, you are checking the urlbox everytime you run the script.
<input type="submit" name="submit" value=" Check IMDB ">
<?php
if(isset($_POST['submit'])) {
//Here add your code
}
?>
No the script will check the urlbox only when the button is pressed.

Related

Deleting multiple rows using checkbox

I have this code
<html>
<head>
<meta charset="UTF-8">
<title> Game Library</title>
<link href="css/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div id="search">
<img id="del" src="img/delete.jpg" alt="Error">
<a class="A" href="index.php"><img class="AR" src="img/src.jpg" title="Search library"></a>
<a class="A" href="insert.php"><img class="AR" src="img/add.png" title="Add game"></a>
<div id="results">
<?php
require("inc/connection.php");
$query = "SELECT * FROM Library";
$result = mysqli_query($conn,$query);
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)){
?><div id="results">
<form action="inc/delete.php" method="GET">
<input type="checkbox" name="checkbox[]" value="<?php echo $row['ID'] ?>">
<p id="p1">Name: <?php echo $row['Name']?>;</p>
<p>Genre: <?php echo $row['Genre']?></p>
<p>Release date: <?php echo $row['Release_date']?></p>
<p>Publisher: <?php echo $row['Publisher']?>:</p>
<p>Platforms: <?php echo $row['Platforms']?></p>
</div>
<?php
}?>
<input type="submit" name = "submit" value = "Submit"></form>
<?php
}else{
echo "No results!";
}
</div>
</div>
</div>
</body>
</html>
And I am supposed to create action="inc/delete.php" script which will allow me to delete multiple rows using checkbox and submit button. Can someone help me out with this please ? I honestly have no clue on how to complete this ....
<?php
if(isset($_POST['submit']))
{
if(!empty($_POST['checkbox']))
{
foreach ($_POST['checkbox'] as $key => $check)
{
// delete query
}
}
}
?>
NOTE: You failed to close PHP at line no. 40. Please make sure your PHP tag is closed or not.
Need to first set form method to `POST'
So, modify
<form action="inc/delete.php" method="GET">
To:
<form action="inc/delete.php" method="post">
And in inc/delete.php,
if (isset($_POST['submit'])){ // Check if form is submitted.
if (! empty($_POST['checkbox'])){ // Check if Checkbox is checked.
// Loop over the checkbox and get selected ids.
foreach($_POST['checkbox'] as $checkbox_id){
echo $checkbox_id."<br/>";
// Add your delete query here.
}
}
}
So the resolution is that you get at your delete script array of ids. You can parse that array and execute DELETE query for each id:
if(isset($_POST['checkbox']))
{
foreach($_POST['checkbox'] as $val)
{
$stmt = $conn->prepare("DELETE FROM Library WHERE id = ?");
$stmt->bind_param('i', $val);
$stmt->execute();
}
}
inc/delete.php
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['checkbox'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['checkbox'] as $row_id){
echo $row_id."</br>";
// DELETE QUERY HERE
}
}
}
?>

Print php output in pop window on submitting the form

I would like to print the output of PHP in a pop window after the submission of html form. Below is sample of my code.
<?php
if(isset($_POST['button1']))
{
if("condition match")
{
echo "output1";
}
else
{
echo "output2";
}
}
?>
<form class='n-form' action='#' method='post'>
<div>
<button type='submit' name="button1" class='button'>Submit</button>
Cancel
</div>
</form>
It should be able to print the echo output in the pop window based on the if condition.
Looking forward to hear.
There is no popout windows in php you have to do it in javascript
<?php
if(!empty($_POST))
{
$text = (isset($_POST['button1'])) ? "output1" : "output2";
echo '<script>alert("' . $text . '")</script>';
}
?>

textarea content auto clears whenever i click the form submit button [PHP]

I have a textarea with a form submit button. Whenever I click the submit button, the content on my textarea clears. but i dont want to clear the content of my textarea. here is my code
codepage.php
<?php
$ans = "hello";
if (isset($_POST['textcode'])) {
{
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="validatePHP">
<textarea name="textcode"></textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>
thanks for the answers! It worked. now i have another question, what if the textarea has already a preloaded text in it and when I type in another text in it and click the submit button, the textarea should have now have the text that i inputted and the preloaded text in the textarea.
here is my updated code
<?php
$ans = "hello";
if (isset($_POST['textcode'])) {
{
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="validatePHP">
<textarea name="textcode"><?php if(isset($_POST['textcode'])) {
echo htmlentities ($_POST['textcode']); }?>hell</textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>
Try render content after submit
<textarea name="textcode"><?= $_POST['textcode']; ?></textarea>
<textarea name="textcode">
<?php if(isset($_POST['textcode'])) {
echo htmlentities ($_POST['textcode']); }?>
</textarea>
Maybe you could do it with $_SESSION?
at the top of your page type session_start();
then inside
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
add the code $_SESSION['textareaMsg'] = $_POST['textcode']; like this...
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
$_SESSION['textareaMsg'] = $_POST['textcode'];
}
then where your text area is set just replace it with this.
<?php
if(isset($_SESSION['textareaMsg'])){
echo '<textarea name="textcode">'.$_SESSION['textareaMsg'].'</textarea>';
}else{
echo '<textarea name="textcode"></textarea>';
}
?>
This works by saving the text area as a session variable when you submit the form, and checking if its set when you load the form, if it is then it will replace the contents of the text area with what is set in the session. Hope this helps!
Try following code
<?php
$ans = "hello";
$textcode = ""; //declare a variable without value to avoid undefined error
if (isset($_POST['textcode'])) {
{
$textcode=$_POST['textcode']; //assign the value to variable in you if statment
if ($textcode == $ans) { //useing variable in if statment
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="test.php">
<!--echo user input -->
<textarea name="textcode"><?php echo $textcode; ?></textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>

sending href with parameter in a loop

<!doctype html>
<html>
<head>
<title>Main Page</title>
</head>
<?php
session_start();
?>
<form action="new_question.php" method="post">
<input type="hidden" name="sid" value="<?php echo $_SESSION['username']?>">
<input type="submit" value="New Question">
</form>
<?php
include ("connection.php");
$result = mysqli_query($con,"SELECT * FROM question_table");
while($row = mysqli_fetch_array($result))
{
echo "" . $row['question'] . $row['q_id'] . "";
echo "<br>";
}
?>
<body>
</body>
</html>
I have 5 question in my database each with a id. this page prints them as a link in loop. upon clicking any of the link it goes to "question.php" file. there i want to echo the question from the database that was clicked previously. the problem is in "question.php" file how do i find out which link was clicked among thus 5. should i send a parameter along with the link? how the parameter will change in each loop? how do i do it in this page? if i do send a parameter with the link how do i receive it in the "question.php" file?
Echo the id as a parameter on the anchor. We can also remove the id from the anchor text since it's not needed there anymore.
while($row = mysqli_fetch_array($result))
{
echo '' . $row['question'] . '<br>';
}
And then in question.php do $_GET['id']

HTML/PHP Survey not passing to MySQL database properly

I'm trying to make a small survey that populates the selections for the dropdown menu from a list of names from a database. The survey does this properly. I want to submit the quote the user submits with this name into a quote database. The quote text they enter into the field goes in properly, however, the name selected from the menu does not get passed in. Instead I get a blank name field.
I understand some of my code is out of context, but the name is the only thing that does not get passed in properly.
On form submit, I include the php file that submits this data to the database:
<form action="<?php $name = $_POST['name']; include "formsubmit.php";?>" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<input type="submit" value="Submit!" />
</form>
The variable $name comes from this (which populates my dropdown menu):
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
echo "</select>";
And here is my formsubmit.php:
<?php:
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
header("Location: quotes.php");
if (#mysql_query($sql)) {
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
Your form action stuff looks weird, but regardless, I think the problem you're having has to do with not setting $name = $_POST['name'] like you're doing with $quotetext = $_POST['quotetext']. Do that before the sql statement and it should be good to go.
edit to try to help you further, I'll include what the overall structure of your code should be, and you should tweak it to fit your actual code (whatever you're leaving out, such as setting $query for your name options):
file 1:
<form action="formsubmit.php" method="post">
<label> <br />What did they say?: <br />
<textarea name="quotetext" rows="10" cols="26"></textarea></label>
<select name='name'>
<?php
while ($temp = mysql_fetch_assoc($query)) {
echo "<option>".htmlspecialchars($temp['name'])."</option>";
}
?>
</select>
<input type="submit" value="Submit!" />
</form>
formsubmit.php:
<?php
mysql_select_db('quotes');
if (isset($_POST['quotetext'])) {
$quotetext = $_POST['quotetext'];
$name = $_POST['name'];
$ident = 'yankees';
$sql = "INSERT INTO quote SET
quotetext='$quotetext',
nametext='$name',
ident='$ident',
quotedate=CURDATE()";
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}
}
?>
echo "<select name='name'>";
while ($temp = mysql_fetch_assoc($query)) {
$nyme = htmlspecialchars($temp['name']);
echo "<option value='$nyme'>$nyme</option>";
}
echo "</select>";-
This way you will receive the value of the name in $_POST array
and you have to get that value out of $_POST array as well you need to change the
code add the following line to get the name in your script.
$name = $_POST['name'];
you need to change the form action tag
<form action='formsubmit.php' .....>
and in that file after successful insertion you can redirect the user to whereever.php.
so it was fun explaining you every thing bit by bit change this now in your code as well.
if (#mysql_query($sql)) {
header("Location: quotes.php");
} else {
echo '<p> Error adding quote: ' .
mysql_error() . '</p>';
}

Categories