Printing html page and doing some action using 1 button - php

I have a Print button which I want to do 2 actions when clicked:
Updating my database.
Printing the HTML page.
This is what I've done so far, but it's not working:
<form action="" method="POST">
<body >
<?php
$n=$_POST['ID'];
$a=implode("</br>",$n);
list($add, $ward) = explode("(!#!)", $a);
?>
<div id="container">
<p id="address">
<?php echo"$address";?>
</p>
<p id="ward">
<?php echo"$ward";?>
</p>
</div>
<input type="submit" name="Print" value="Print" />
<?php
if(isset($_POST['Print']))
{
?><script>javascript:window.print()</script><?php
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
}?>
<div id="footer">
</div>
</form>
After using this print button , my database is getting updated, but the print window is showing error(i.e the variables posted from other page are showing errors).
Can anyone please help me print and update at same time with this Print button?

If your page does not have it, add form tags. They are mandatory unless you want to AJAX the data. Also remove the semi-colon from the end of the sql
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php
if(isset($_POST['Print'])) {
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?><script>window.print();</script>
<?php } ?>
UPDATE: Perhaps you mean this, but I do not want to keep correcting HTML.
<?php
$n=$_POST["ID"];
$a=implode("</br>",$n);
list($add,$ward)=explode("(!#!)", $a);
?>
<body>
<div id="headerbg">
<div id="header-e1"><a align="left" href="escalationReport.php">Back </a>
</div>
<div id="header-e3"><a align="right" href="logout.php">Logout </a>
</div>
<h1><p>Issue Notice</h1>
</div>
<div id="container">
<p id="address">
<?php echo "$address";?>
</p>
<p id="ward">
<?php echo "$ward";?>
</p>
</div>
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php if(isset($_POST[ 'Print'])) {
mysql_query( "UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?>
<script>
window.print();
</script>
<?php } ?>
<div id="footer"></div>
</body>

you have an error in the update query, you have placed semicolon inside the query so please remove that so the query will be like this.
Make sure your form method is POST
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");

Make sure you are using input type button inside the form tag. And use post method for form.

Related

unable to check the query by if statement

I want to check if the path of the image is NULL or not but the if statement after my select query as shown below doesn't work; the code directly passes to else statement and displays the div in else statement, so anyone who can help me please...
<div class="adminLeft">
<?php
$userID=$_SESSION['userID'];
$sql="SELECT path from cim WHERE userID='$userID'";
$res=mysql_query($sql);
if ($res==NULL)
{
?>
<div class="card">
<p> <img class ="card-img-top" src="/images/picture-profile.jpg"></p>
</p>
</div>
<br>
<form id="uploadprofpic" action="up.php" enctype="multipart/form-data"
method="post">
<p> <input type="file" name="pic" accept="image/*"></p>
<input name="submit" class="formbutton" value="Upload your Photo"
type="submit" id="submitReport"/>
</form>
<?php }
else {?>
<div class="card">
<p> <img class ="card-img-top" src=""></p>
}
?> </p>
</div>
You are checking for a string "NULL", changing it to just NULL will check if it returned NULL
<?php
if ($res == NULL){
?>
<div class="card">
<p> <img class ="card-img-top" src="/images/picture-profile.jpg"></p>
<?php }?>
</div>
So now if it returns NULL it will show the div.
By the way you are not closing the php properly in the end, you have >? it is supposed to be ?>
Try is_null function like this :
if (is_null($res)){}
use simply null not "NULL"
if ($res==null){
}

Show div onclick function using jQuery

I am new in web development. I am trying to show div onclick function so I need some help.
here is my code
<div id="editdiv<?php echo $consultnt->id?>" style="display:none">
<p> edit</p>
</div>
<?php if(trim($consultnt->status) !="completed"){ ?>
<div class="pull-right" id="div_chng_hr">
<input type="submit" name="edit_hour" onClick="showEdit(<?php echo "editdiv".$consultnt->id ?>)" on value="Edit Hour">
</div>
<?php } ?>
and script code here is
function showEdit(id){
$('#editdiv').show();
}
I inspect edit hour button then I have value onclick
<input type="submit" name="edit_hour" onclick="showEdit(editdiv26)" value="Edit Hour">
and div id is
<div id="editdiv26" style="display:none">
tell me where I am doing wrong. Thanks in advance
Since you are giving the function a param
onClick="showEdit(<?php echo " 'editdiv".$consultnt->id." ' "?>)"
eter it shoud be between ' '
So the onClick function should be
And in your function use "#" + id instead of #editdiv
function showEdit(id){
$('#editdiv' + id).show();
}
Your div have different id that "editdiv" because it's extended by consultant id. So you have to use consultant id in your function as well.
Concat the ID properly. Since you have a static editdiv and a dynamic ID use $('#editdiv' + id)
function showEdit(id) {
$('#editdiv' + id).show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="editdiv1" style="display:none">1 </div>
<div class="pull-right" id="div_chng_hr">
<input type="button" name="edit_hour" onClick="showEdit(1)" on value="Edit Hour">
</div>
first thing is make sure you link a jQuery library
<script src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
second thing is code
<div id="editdiv<?php echo $consultnt->id?>" style="display:none">
<p> edit</p>
</div>
<?php if(trim($consultnt->status)!='completed'){ ?>
<div class="pull-right" id="div_chng_hr">
<input type="submit" name="edit_hour" onClick="showEdit(<?php echo $consultnt->id?>)" on value="Edit Hour">
</div>
<?php } ?>
<script>
function showEdit(id){
$('#editdiv'+id).show();
}
</script>
Use this html: remove 'editdiv' text from onClick event
<div id="editdiv<?php echo $consultnt->id?>" style="display:none">
<p> edit</p>
</div>
<?php if(trim($consultnt->status) !="completed"){ ?>
<div class="pull-right" id="div_chng_hr">
<input type="submit" name="edit_hour" onClick="showEdit(<?php echo $consultnt->id ?>)" on value="Edit Hour">
</div>
<?php } ?>
script part:
<script>
function showEdit(id){
$('#editdiv'+id).show();
}
</script>
Please have a look at here working example
`https://jsfiddle.net/yhpduthm/10/`
I prefer in function you have to add only ID and that will use in function.

PHP redirect form to URL not working

So I'm trying to use this http://www.formget.com/how-to-redirect-a-url-php-form/ as an RSVP form.
Ideally, entering the right code on (http://baby.engquist.com/invite/) will lead you to a google form. However, when I enter any code (right or wrong) and press the button, it simply refreshes back to the /invite page.
My code is as follows:
<p style="text-align: center;">
<form action="index.php" id="#form" method="post" name="#form">
<div class="row">
<div class="large-3 columns large-centered">
<div class="row collapse">
<div class="small-10 columns">
<input id="code" name="code" placeholder="Enter the code to RSVP." type="text" >
</div>
<div class="small-2 columns">
<input id='btn' name="submit" type='submit' class="button prefix" value='Go'>
</div>
</div>
</div>
</div>
<?php
include "redirect.php";
?>
</form>
</p>
And the included redirect.php:
<?php
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$code = $_POST['code'];
if($code ='show620')
{
// To redirect form on a particular page
header("Location:http://google.com/");
} else {
print "Oops that's not the right code. Try again!";
}
?>
Thanks so much for any help!
You should have action attribute pointing to file where you do processing after submitting. In your case its redirect.php
Use :
<form action="redirect.php" > ............
And dont include redirect.php at the bottom of the form.
You need to write ob_start(); on top of your page and die(); after header("Location:http://google.com/"); in redirect.php
The php header redirect only works if it's called from a page that is completely blank. You have to change your form action to "redirect.php" and simply get rid of the code at the bottom of your html.

php function Not Displaying form

Im having a problem displaying a php function. Its for an admin log in form.
The Function Look Like this -
function displayAdmin(){
//test if login is valid
if (isset($_SESSION['adminLogin'])){
if($_SESSION ['adminLogin']=="valid"){
?>
<script type="text/javascript">location.replace('addproduct.php')</script>
<?php
}
else {
// test if login is invalid
// display error message and login form
if($_SESSION['adminLogin']=="invalid") {
echo "<div>Incorrect User ID and/or password provided</div>";
?>
<form name="adminLogin" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div id="sign_up_form">
<label><strong>Username:</strong> <input type = "text" name="userID" class="sprited"/></label>
<label><strong>Password:</strong> <input type="password" name="passWord" class="sprited"/></label>
<div id="actions">
<a class="close form_button sprited" id="cancel" href="#">Cancel</a>
<a type ="submit" name="adminSignin"class="form_button sprited" id="log_in" href="">Sign in</a>
</div>
</div>
</form>
<?php
}
}
?>
<form name="adminLogin" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div id="sign_up_form">
<label><strong>Username:</strong> <input type = "text" name="userID" class="sprited"/></label>
<label><strong>Password:</strong> <input type="password" name="passWord" class="sprited"/></label>
<div id="actions">
<a class="close form_button sprited" id="cancel" href="#">Cancel</a>
<a type ="submit" name="adminSignin"class="form_button sprited" id="log_in" href="">Sign in</a>
</div>
</div>
</form>
<?php
}
} // end of function
And on my page where I am wanting the function to sit the code looks like this -
<?php
session_start();
// Test that page title has been created
if (!isset($pageTitle)) {
$pageTitle = '<< Page title not set >>';
}
// include the myFunctions file
include('includes/myFunctions.php');
// test if login details have been keyed in
if(!empty($_POST["userID"])) {
// Store userID and passWord in local variables
$userID=$_POST["userID"];
$passWord=$_POST["passWord"];
// check database for valid customer
checkValidAdmin($userID, $passWord);
}
?>
and then -
<div id="sign_up">
<h3 id="see_id">Administration Log in</h3>
<span>Please sign in using the form below</span>
<div><?php displayAdmin(); ?></div>
<a id="close_x" class="close sprited" href="#">close</a>
</div>
I have searched long and hard for this problem but can not seem to find the issue, if the issue jumps out at anyone I would love to hear from you!
Thank you so much in advance!!
Definitely your $_SESSION ['adminLogin']=="valid" condition is not true. Check it is set properly or session_start(); is called at beginning of the script.
The problem is you did'nt return the html string.
You should assign the form to a variable, and at the end of the function , return this variable.
$_SESSION['adminLogin'] value is empty i.e. not set or null. that is the reason function inside condition not satisfied.

submit form to page and depending on input show different div

I Have a form which when submitted needs to go to the page and then show one of 4 hidden divs depending on the page.
Here is the form
<form>
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Here is the page
<div id="brighton">
<p>Brighton</p>
</div>
<div id="devon">
<p>Devon</p>
</div>
<div id="search">
<p>search</p>
</div>
<div id="variety">
<p>variety</p>
</div>
So if Brighton is typed into the place input i need the form to submit the page and show the Brighton div and if Devon is typed in to show the Devon div etc and if the 2/12/2012 is typed into the date picker input and Brighton into the place input it goes to the page and shows the variety div.
i also need it so if the 1/12/2012 is typed in to the date picker input the page redirects to the page show.html.
any help would be greatly appreciated
thanks.
This is easy if you know PHP at all. It looks like you need a good, easy start. Then you will be able to achieve this in seconds.
Refer to W3SCHOOLS PHP Tutorial.
To achieve what you have mentioned, first make the following changes in your form:
<form action="submit.php" method="post">
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Create a new file called submit.php and add the following code:
<?php
$place = $_POST['place'];
$date = $_POST['datepicker'];
if ($date == '1/12/2012') {
header('Location: show.html');
exit;
}
?>
<?php if ($place == 'Brighton''): ?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php elseif ($place == 'Devon'): ?>
<div id="devon">
<p>Devon</p>
</div>
<?php elseif ($place == 'search'): ?>
<div id="search">
<p>search</p>
</div>
<?php elseif ($place == 'Variety'): ?>
<div id="variety">
<p>variety</p>
</div>
<?php endif; ?>
Now the above example is not the complete solution, but it gives you an idea as to how you can use if-then-else construct in PHP to compare values and do as desired.
Post your form to a php page and then check the posted form parameters to determine which div to show.
<?php
if ($_POST["place"] == "Brighton") {
?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php
} else if ($_POST["place"] == "Devon") {
?>
<div id="devon">
<p>Devon</p>
</div>
<?php
}
?>
Do that for each div and parameter combination. Make sure you set the "method" attribute on your form to "post":
<form action="somepage.php" method="post">...</form>
In the resulting HTML you will only see the one that matches the form parameter.

Categories