I am trying to build a webapp which shall filter results based on the users current button select and slider choice, I will be achieving this via PHP, SQL and AJAX calls but I need help with sending the correct data to my URL.
DB Dip -
$pType = $_GET['s'];
$fPsiVal = '15000';
$fGpmVal = '10000';
$hPsiVal = '30000';
$hGpmVal = '10000';
$sql = "SELECT * FROM pumps
WHERE pump_type = '$pType'
AND flow_psi <= '$fPsiVal'
AND flow_gpm <= '$fGpmVal'
AND high_psi <= '$hPsiVal'
AND high_gpm <= '$hGpmVal'";
I want to set a default value for pType of ?pType=CONTINUOUS instead of having a static variable, how would I pass this to my URL when the page is called initially?
In order to handle user result filtering I created a form with the method of GET which as I currently understand should send the data to the URL upon for submission, however nothing happens and I am not quite sure what I have done wrong
GET Form -
<form name="pumpCat" action="?s=<?php echo $pType ?>" method="get" align="center">
<div class="btn-group" data-toggle="buttons-radio">
<button type="submit" class="<?php if( $pType == 'intermittent' ){ echo 'active '; } else echo 'noClass '; ?>btn btn-primary waitingUiBut" id="but1" name="s" value="intermittent" action="pumpCat">INTERMITTENT</button>
<button type="submit" class="<?php if( $pType == 'continuous' ){ echo 'active '; } else echo 'noClass '; ?>btn btn-primary waitingUiBut" id="but4" name="s" value="continuous"action="pumpCat">CONTINUOUS</button>
</div>
</form>
Any pointers would be greatly appreciated.
Your action="?pType=" seems to set the default value properly
But, there is no 'href' attribute in a button. Try using an element with proper href and use that to submit the form.
To fix this problem I simply added a query string on the navbar link
<li class=" animateNav <?php if ($pageCat == "Well Service"){ echo "active"; } ?>"><a id="link" href="wellservice.php?s=intermittent">WELL SERVICE</a></li>
and also amended the forms using GET to change the query parameters on the buttons onclick event
<form name="pumpType" action="?s=<?php echo $pType ?>" method="get" align="center">
<div class="btn-group" data-toggle="buttons-radio">
<button type="submit" class="<?php if( $pType == 'intermittent' ){ echo 'active '; } ?>btn btn-primary waitingUiBut" id="but1" name="s" value="intermittent" action="pumpCat">INTERMITTENT</button>
<button type="submit" class="<?php if( $pType == 'continuous' ){ echo 'active '; } ?>btn btn-primary waitingUiBut" id="but4" name="s" value="continuous"action="pumpCat">CONTINUOUS</button>
</div>
</form>
If I understand your goals correctly, I would rethink your apps structure.
You mention you want to use AJAX, so I'd have my SQL query in a separate PHP file, then post the click or slider function value to that file using jQuery, something like:
jQuery
Then update your main HTML file with the returned values.
There is a basic example:
PHP MySQL AJAX
Related
i wanna add a confirmation message when i wanna delete an item showed in my webpage that's connected to a database
<?php
if (isset($_GET['Pdelete'])) {
$delid = sanitize($_GET['Pdelete']);
$db->query("DELETE FROM products WHERE ID = $delid");
header('Location: Archived.php');
}
?>
<i class="far fa-trash-alt"></i>
these codes work fine, all i need now is a confirmation message. TIA
Your best option is to use javascript. I really like to use a jQuery library called Jquery Validator. This library is very convenient to use and has a lot of other very powerful tools for validating a input form. That would be my first choice.
If you are looking for a pure php way of doing this you can use something like this.
if(isset($_POST['yes'])){
$delid = sanitize($_POST['yes']);
$db->query("DELETE FROM products WHERE ID = $delid");
header('Location: Archived.php');
}
if(isset($_POST['no'])){
header('Location: whereever.php'); //Redirect to whereever.
}
if(isset($_GET['Pdelete'])){
echo
'<form action="" method="post" enctype="multipart/form-data">
<p>Are you sure that you want to delete this item?</p>
<input type="submit" name="yes" value="' . $_GET['Pdelete'] . '"> <input type="submit" name="no" value="No">
</form>';
exit();
}
Use this code i think i have helped you.
<i class="far fa-trash-alt"></i>
I'm trying to display one of two buttons depending on "magnum"(printername) being in present in the array "booleans".
My problem is that when the form gets posted, the data retrieved on page load is correct, but the buttons displayed are incorrect. if clicked on a button, the form posts and refreshes the page, "magnum" gets pushed into $_SESSION['booleans'] but the button still displays "btn btn-default", so it requires another page refresh for the button to load correctly('btn btn-succes').
Is my problem due to $_SESSION or am i missing something?
echo'
<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
if(isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']){
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans'][$pos]);
dump('unset');
}
}
elseif(!in_array('magnumBool',$_SESSION['booleans'])){
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
if(isset($_POST['submitMagnum'])&& $_POST['submitMagnum']){
array_push($_SESSION['booleans'],'magnumBool');
dump('set');
}
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '
</div>
</form>';
$_SESSION['printers'] is an array containing "magnum" -
$_SESSION['booleans'] is the array which isn't working as i would like it to -
$enabled_printer = "btn btn-success" <br>
$disabled_printer = "btn btn-default" <br>
$lost_connection_printer = "btn btn-danger disabled"
The problem is that you are mixing elaboration and printing. Try to split you code, so it will work and it will be more readable:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['unSubmitMagnum']) && $_POST['unSubmitMagnum']) {
$pos = array_search('magnumBool', $_SESSION['booleans']);
unset($_SESSION['booleans']['magnumBool']);
} elseif (isset($_POST['submitMagnum'])&& $_POST['submitMagnum']) {
$_SESSION['booleans']['magnumBool'] = true;
}
}
echo'<form class="form1" method="post" action="" id="form1">
<div class="col-xs-offset-1 col-xs-2">';
if(in_array('magnum', $_SESSION['printers'])){
if(isset($_SESSION['booleans']['magnumBool'])){
echo '<input type="submit" name="unSubmitMagnum" id="magnumBool" value="magnum" class='.$enabled_printer.'>';
} else {
echo '<input type="submit" name="submitMagnum" id="magnumBool" value="magnum" class='.$disabled_printer.'>';
}
}
else{
echo '<button id="magnum" class='.$lost_connection_printer.'>1. Magnum</button>';
}
echo '</div>
</form>';
P.s. note the use of "magnumBool" as a key isset instead of as a value: in this way (when possible) you will avoid duplicate entry and will make you code lighter if you have large arrays ;)
P.p.s. try always to keep you login separate from you template, this will make your code more readable and easier to maintain
I have two php page.I'm using Bootstrap, php and mysql. In the first page I load three objects into div from mysql database of the user logged. To do this I'm using the next code:
<div class="container">
<div class="row">
<?php
require_once('function.php');
conectar('localhost', 'root', '', 'mydb');
$consulta1 = mysql_query('SELECT id FROM user WHERE username="'.$_SESSION["name"].'"');
$result = mysql_query('SELECT * FROM finc WHERE Usuario_idUsuario="'.$_SESSION["idUser"].'"');
if ($row = mysql_fetch_array($result)){
do{
echo '<div class="col-lg-4">' ;
echo '<img class="center-block img-circle" src="data:image/gif;base64,R0lGODlhOw=="
alt="Generic placeholder image" style="width: 140px; height: 140px;">';
echo '<h2 class="text-center">'.$row['name'].'</h2>';
echo '<p align="center">'.$row['data'].'</p>';
echo '<p align="center">'.$row['tao'].'</p>';
echo '
<a type="button" class="btn btn-success" href="secondPage.php" role="button">Entrar »</a>
';
echo'</div>';
}while ($row = mysql_fetch_array($result));
} else {
echo "¡ No data for this user!";
<a}
?>
<!-- /.col-lg-4 -->
</div>
<!-- /.row -->
</div>
I need send the id value depending of the button clicked for load the data associated in the next php page. For example, If I click in the second button created in the do-while loop I need send the id=2 to the sencondPage.php. I have searched how to do this, but only find how to send var into url like sencondPage.php?var=2, And I hate this because user can change url... And adding value into session, but on click I haven't get this.
So, how can I pass the corresponding id value when user click in the link??
Thanks!
You could use an html <form>:
<form method="post" action="[URL FOR NEXT PAGE]">
...
<input type="submit" name="value1" value="Button 1" />
<input type="submit" name="value2" value="Button 2" />
</form>
Now if someone clicks the first button it will send them to the next page with the post data: value1=Button%201, and if they click the second button the it will instead be value2=Button%202. In either case any other form elements inside the form will also be submitted via post. With PHP you can retrieve these values using something like:
if ($_POST['value1']) {
...
elseif ($_POST['value2']) {
...
}
I know its a duplicate one but i'm getting this error while trying to fetch data passed from a link..I dont know how to resolve it.
here is my code:
add_package.php
echo "<td><a href='delete.php?name3=" . $row['package_type']."&id3=".$row['p_id']."'>Delete</a></td>";
echo "<td><a href='edit_package.php?name3=" . $row['package_type']."&id3=".$row['p_id']."'>Update</a></td>";
here the delete link works perfectly but when i click update it takes to the edit_package page where i'm getting an undefined error..
code for edit_package.php:
<?php
include('db.php');
$id4 = $_GET['id3'];//update the page
$name4 = $_GET['name3'];//helps to update the package
echo $id4;
echo $name4;//getting values here correctly..
if(isset($_POST['submit']) )
{
$package=$_POST['package'];
if (ctype_alnum($package) && !empty($id4) && !empty($name4))
{
$sql13="select package_type,id from tbl_package where package_type='".$package."'";
$retvali=mysql_query($sql13,$conn);
$num_rows1 = mysql_num_rows($retvali);
if ($num_rows1 == 0 || $num_rows1=="")
{
$sql = "Update tbl_package set package_type='".$package."' where package_type='".$name4."' and p_id='".$id4."'";
$retval = mysql_query( $sql, $conn );
?><script>alert("Updated Successsfully");window.location ='http://localhost/demo/add_package.php';
</script><?php
}
else
{
?><script>alert("Already Exists");window.location ='http://localhost/demo/add_package.php';
</script><?php
}
}
else
{
?><script>alert("enter only letters and numbers")</script><?php
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<form id="form-validation" action="edit_package.php" method="post" class="form-horizontal" enctype="multipart/form-data" novalidate="novalidate">
<div class="col-md-6">
<div class="block" style="height:500px;">
<div class="block-title">
<h2><strong>State the Package For Tour</strong></h2>
</div>
<fieldset>
<div class="form-group">
<label class="col-md-4 control-label" for="val_username">Update Package <span class="text-danger">*</span></label>
<div class="col-md-6">
<div class="input-group">
<input type="text" id="package" name="package" class="form-control" required >
<span class="input-group-addon"><i class="fa fa-user"></i></span>
</div>
</div>
</div>
<div class="form-group form-actions">
<div class="col-md-8 col-md-offset-4">
<input type="submit" class="btn btn-info btn-primary " value="Update" name="submit">
</div>
</div>
</fieldset>
</form>
When i press update button i'm getting an undefined error i dont know why?..Thanks in advance
I'm attaching an image to it..
Try to change the <form>'s action URL to include your GET varaibles:
<form id="form-validation" action="edit_package.php?id3=<?php echo $_GET['id3']; ?>&name3=<?php echo $_GET['name3']; ?>" method="post" class="form-horizontal" enctype="multipart/form-data" novalidate="novalidate">
PLEASE NOTE: This is extremely unsafe! You need to sanitize ALL user input before using it. My example above, dis-regards security, and simply is to demonstrate my point. GET and POST data, are user variables. A malicious user could put bad code in the URL (ie ?name3=<badcode>) and it would be printed on the page, well in the source code, which they could easily pop out of. Also, in SQL queries, you need to escape the data or use prepared statements.
You should not be using mysql functions, switch to MySQLi or PDO. MySQL has been killed for a while now..
These are just asking for you to get hacked:
$sql13="select package_type,id from tbl_package where package_type='".$package."'";
and..
$sql = "Update tbl_package set package_type='".$package."' where package_type='".$name4."' and p_id='".$id4."'";
You are vulnerable to SQL injections, would could easily allow a malicious attacker to add/edit/view/delete data in your database.
The problem is, you have $package (which is raw data from POST) and $id4 and $name4 (which is raw data from GET) in your SQL query.
You would use mysql_real_escape_string() on them, but you should be using mysqli or PDO anyways...
Example:
$name4 = mysql_real_escape_string($_GET['name3']);
It's confusing, I don't know what the GET variable is called name3 but you assign it the variable $name4.. Whoever (even you) comes along later on will be lost in your code.
Updated:
Try this code. I swapped your GET for POST in your php code, and passed the GET variables from your URL as hidden fields in your form.
<?php
include('db.php');
if(isset($_POST['submit']) )
{
$package = mysql_real_escape_string($_POST['package']);
$id4 = mysql_real_escape_string($_POST['id3']); // why is variable named id4 but its id3??
$name4 = mysql_real_escape_string($_POST['name3']); // why is variable $name4 but its name3??
if (ctype_alnum($package) && !empty($id4) && !empty($name4))
{
$sql13 = "SELECT package_type,id FROM tbl_package WHERE package_type='$package' LIMIT 1";
$retvali = mysql_query($sql13, $conn);
$num_rows1 = mysql_num_rows($retvali);
if ($num_rows1 == 0 || $num_rows1=="")
{
$sql = "Update tbl_package set package_type='$package' WHERE package_type = '$name4' AND p_id='$id4'";
$retval = mysql_query( $sql, $conn );
echo '<script>alert("Updated Successsfully");window.location = "http://localhost/demo/add_package.php";</script>';
} else {
echo '<script>alert("Already Exists"); window.location = "http://localhost/demo/add_package.php";</script>';
}
} else {
echo '<script>alert("enter only letters and numbers");</script>';
}
}
?>
<form action="edit_package.php" method="post" enctype="multipart/form-data" novalidate="novalidate">
<input type="hidden" name="id3" value="<?php echo htmlspecialchars($_GET['id3'], ENT_QUOTES | ENT_HTML5); ?>" />
<input type="hidden" name="name3" value="<?php echo htmlspecialchars($_GET['name3'], ENT_QUOTES | ENT_HTML5); ?>" />
Update Package: <input type="text" id="package" name="package" class="form-control" required >
<input type="submit" class="btn btn-info btn-primary " value="Update" name="submit">
</form>
I removed your HTML formatting from the form. You had div tags that didn't match up.. I can't see your whole code, but it looks like you have a bunch of div's that are messed up (ie: not closed where they should be). I also added mysql_real_escape_string() to the passed variables, and htmlspecialchars() to the GET variables echo'd in the hidden fields of your form. It's a start.
You might be able to make better sense of your code and troubleshoot errors, if you wrote your code a bit cleaner. Not trying to bash you :) Proper indentation, spacing, and formatting go a long way. It makes it easier on your eyes, and on yourself, in times like these..
I left your <script> tags because I assumed there was a reason your wanted to popup a message box.. I would just use header('Location: /path/to/where.php'); and pass your error message through a session variable or something, like an array of errors, which you get, clear, and show on the page the errors.
What would be the best way to display a success message after submitting a form and then redirecting.
I'm developing a script with smarty template engine and i don't want to use javascript as i want it to be as easy as possible for a user to design his or her own template and i have tried using sessions to display the message it seems to unset after redirecting for example
The Form
<form action="" id="comment_form" method="POST">
<input type="hidden" name="movie_id" id="page_id" value="{$mov.mid}" />
<input type="hidden" name="user_id" id="user_id" value="{$logged_id}" />
<input class="span2" name="comment" type="text" />
<button class="btn" name="add_comment" type="submit">Add Comment</button>
</form>
PHP
if (isset($_POST['add_comment'])) {
if (!empty($_POST['comment'])) {
$user_id = $logged_id;
$post_comment = mysqli_real_escape_string($con, $_POST['comment']);
$movie->AddMovieComments($con, $movie_id, $user_id, $post_comment);
$_SESSION['insert'] = 'success';
header ('Location: '.$_SERVER['REQUEST_URI']);
} else {
$_SESSION['insert'] = 'empty';
}
}
Now trying to display the success in tpl
{if isset($smarty.session.insert)}
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<h4>Success</h4>
Your Comment was successfully added
</div>
{/if}
{php}unset($_SESSION['insert']);{/php}
So can anyone help me out and tell me the best way to do this or tell me why it is unset before it is displayed.
Try
if( isset($_SESSION['insert']) && $_SESSION['insert'] == 'success')
{
//Your alert
unset($_SESSION['insert']);
}
I don't think a SESSION variable is the best way to do this.
In our template, have a simple condition check to see if a Smarty variable exists:
{if $success}Your comment was added{/if}
Then, in your PHP that handles the transaction, simple initiate the variable, set it to true or false depending on the outcome, and assign it in Smarty before your template is displayed
$success = false;
if (!empty($_POST['comment'])) {
$success = true;
}
$smarty->assign("success",$success);
$smarty->display("template.tpl");
Of course, should verify that the insert was successful before you make $success = true, but you get the drift.