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>';
}
?>
Related
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.
I can't figure this out alone. I have .html file and in body section I'm importing some .php files using switch. I'm displaying table and I'm navigating on it with POST form. Now I want to add to my navigation bar search input which also will send data by POST. It is located on head tag and when I press the button nothing really happens.
index.php
<html>
<head>
<div class="leftNav">
<a class="buttonNav" href="index.php?subpage=table&data=session&page=0">Session</a>
<a class="buttonNav" href="index.php?subpage=table&data=all&page=0">All</a>
</div>
<div class="rightNav">
<?php include 'navbars.php' ?>
</div>
</head>
<body style="font-family:Verdana;">
<div class="content">
<?php
if(isset($_GET['subpage']))
{
switch($_GET['subpage'])
{
case 'table':
include 'datatable.php';
break;
default:
include 'home.php';
break;
}
}
else include 'home.php';
?>
</div>
</body>
</html>
datatable.php
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['search'])
{
$filtr = $_POST['filtr'];
$data = $_GET['data'];
header("
Location: index.php?subpage=table&data=".$data."&page=0&search=".$filtr);
}
else{...}
}
//DISPLAYING TABLE//
echo("
</table>
<form class='nav' method='POST'>");
$disabled = '';
if($curr+1 == 1)
$disabled = "disabled='disabled'";
echo("<input type='submit' name='f' value='First page' ".$disabled.">
<input type='submit' name='p' value='Previous page' ".$disabled."> ");
$disabled = "";
if($curr == $max)
$disabled = "disabled='disabled'";
echo("<input type='submit' name='n' value='Next page' ".$disabled.">
<input type='submit' name='l' value='Last page' ".$disabled."></form>");
?>
navbars.php
<?php
if(isset($_GET['subpage']) && $_GET['subpage'] == 'table')
{
echo("
<form method='POST'>
<input type='text' name='filtr'>
<input type='submit' name='search' value='Search'>
</form>
");
}
?>
As noted in the other answer, you will want to use the action attribute but you'll want to pass the $_GET['subpage'] as in:
action="<?php if(!empty($_GET['subpage'])) echo '?subpage='.htmlspecialchars($_GET['subpage'], ENT_QUOTES) ?>"
Note: I don't see anywhere that you actually send the $_GET['subpage'] or $_GET['data'] in your scripts in the first place, so I don't know if that is even being called properly.
Try Adding form action if you are referring to same page,
action="<?php echo $_SERVER["PHP_SELF"];?>"
hope it helps you.
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>
I have this php code which hide the download button after clicking on it one time by changing the ID from 0 to 1 .. after that if another time the user signed in , it removes the button using a simple css hide code.
here is my code :
<?php
$result = #mysql_query("SELECT * FROM scode WHERE updated= 1 and coden ='$username'");
if ($_POST[downloadTheFile]== "downloadTheFile")
{
$upd_art = "update scode set downloaded='".$_POST[t11] ."' where id='$_SESSION[userid]'";
mysql_query($upd_art) or die(mysql_error());
}
if($row['downloaded']==1)
{
echo "<style>
.thedownloadbutton {display:none;}
</style>";
}
?>
<form class="thedownloadbutton" method="get" action="<? echo '../download/'.$item_downloadlink .'.zip' ; ?>">
<button type="submit" name="downloadTheFile" value="downloadTheFile">Download </button>
<input name="t11" type="hidden" size="2" value="1">
</form>
(just for clarifying: updated =1 is a field that'll open the download page.. if the updated =1 then there is a download page )
I don't know why it doesn't work ..
can you help me please and till me which part is the wrong part ?
I know it's a bad way to hide an element using css , is there another suggestion ?
$_POST[downloadTheFile] should be $_POST['downloadTheFile']
if($row['downloaded']!=1)
{
echo '<button type="submit" name="downloadTheFile" value="downloadTheFile">Download </button>';
}
so there were some problems in this code .. it wasn't professional at all so I used this new code which has a button that update the number from 0 to 1 on the database
(that will tell the download button to not show after that)
then shows the download button
<?
$res = mysql_query("SELECT downloaded FROM scode WHERE id='$_SESSION[userid]'");
$row = mysql_fetch_array($res);
// echo $row['downloaded'];
echo '<br>';
?>
<form method="POST" action=''>
<?
if($row['downloaded'] ==='0')
{
echo "<input type='submit' name='button1' value='click to show the download link' onclick ='validatea(); return false;' />";
//echo '<button type="submit" formmethod="post" name="downloadTheFile" value="downloadTheFile">Download </button>';
?>
</form>
<?
if (isset($_POST['button1']))
{
?> <form class="aaa" method="POST" action="<? echo "../download/".$item_downloadlink .".zip" ; ?>">
<? ;
?> <input type='submit' name='submit' value='download' onclick ='validate(); return false;' /> <? ;
echo"</form>";
$upd_art = "update scode set downloaded='1' where id='$_SESSION[userid]'";
mysql_query($upd_art) or die(mysql_error());
}
}
else {
echo "you already downloaded the file , if you have any problem please contact us";
echo"<br>";
}
?>
<?php
if($dun==0){
show form1 and add vaules to database
}
else if($toplam==0){
show form2 and add values to database
}
else if($toplam==$dun){
header('Location: member.php');
}else{
echo "<script> alert('Error.');history.go(-1);</script>";
}
?>
How i can show two different from by the if statement
You could use inline HTML by closing the <?PHP tag with ?> like this:
<?php
if($dun==0){
// start parsing HTML
?>
<form>[...]</form>
<?PHP
} else if($toplam==0){
// start parsing second HTML
?>
<form>[...]</form>
<?PHP
}
else if($toplam==$dun){
header('Location: member.php');
}else{
echo "<script> alert('Error.');history.go(-1);</script>";
}
?>
If OOP is not used and you are unfamiliar with OOP programming and MVC framework or html rendering then you could do the following:
form1.php file holds the HTML for form 1
form2.php file holds the HTML for form 2
if ($dun == 0) {
include('form1.php');
//add values to database
} else if ($toplam == 0) {
include('form2.php');
//add values to database
}
Couldn't you just echo the forms the same way you echo'd the javascript?
<?php
if($dun==0){
echo '
<form name="form1"><input type="submit" name="submit1" /></form>
';
}
else if($toplam==0){
echo '
<form name="form2"><input type="submit" name="submit2" /></form>
';
}
else if($toplam==$dun){
header('Location: member.php');
}else{
echo "<script> alert('Error.');history.go(-1);</script>";
}
?>
And if you wanted to insert data into your database depending on the if statements then you could add functions:
<?php
function doForm1(){
//MYSQL HERE
}
function doForm2(){
//MYSQL HERE
}
if($dun==0){
echo '
<form name="form1"><input type="submit" name="submit1" /></form>
';
doForm1();
}
else if($toplam==0){
echo '
<form name="form2"><input type="submit" name="submit2" /></form>
';
doForm2();
}
else if($toplam==$dun){
header('Location: member.php');
}else{
echo "<script> alert('Error.');history.go(-1);</script>";
}
?>