Pagination with search function - php

What I'm trying to do is make a search page where the result would be on the same page. My concept is to first show all data and then there will be one textbox and one submit button. Below that, I will show the result which is paginated. How do I achieve that?
<input type ="text" placeholder = "search" name = "search">
<input type ="submit" name = "submit">
<?php
if(isset($_POST['submit']))
$search = $_POST['search'];
{
$query = "SELECT * FROM table WHERE search = '$search' ";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_array($result))
{
//echo my result here.
}
}

Take a look at jquery and ajax. I did something similar as a test awhile back:
var request = $.ajax({
type: "GET",
url: url,
data: $(".form-wrapper").serialize()
});
request.done(function(data) {
if (data.search("No results") == -1) {
$("#result").show();
$("#result").html(data);
} else {
$('#result').html('').hide();
}
});
request.fail(function(jqXHR, msg) {
alert(msg);
});
<form class="form-wrapper cf">
<input type="text" id="text" name="q" placeholder="" required>
<button type="submit" disabled>Search</button>
</form>
<div id="result"></div>

Related

ajax like button success incorrect

I was making like button with php and ajax/jquery and it wont be reload when you click it will just reload the div but it reload the div but the number of like don't add
here my code.
<div id="Post-ImVi-Action">
<script>
$(document).ready(function () {
$('#Post-ImVi-Action-Like').click(function (e) {
e.preventDefault();
var name = $('#Like_Poster_name').val();
var email = $('#Like_Post_1').val();
$.ajax
({
type: "POST",
url: "php-SocialMedia-Posting-Like.php",
data: { "Poster_name": name, "Post_1": email },
success: function (data) {
$("#Post-ImVi-Action-Like").html(data);
}
});
});
});
</script>
<input type="Hidden" name="Poster_Name" id="Like_Poster_name" value="<?php echo $Post_Row["Poster_Name"];?>">
<input type="Hidden" name="Post_1" id="Like_Post_1" value="<?php echo $Post_Row["Post_1"];?>">
<input type="Hidden" name="ID" id="Like" value="<?php echo $Post_Row["ID"];?>">
<button id="Post-ImVi-Action-View">158K</button>
<button id="Post-ImVi-Action-Like" type="button" name="Like_Button" >
<?php
$Post = $Post_Row["Post_1"];
$Sql_like_count = "SELECT * FROM `likes` WHERE post = '$Post'";
$Result_like_Count = mysqli_query($conn, $Sql_like_count);
while ($ROW_LIKE = mysqli_fetch_array($Result_like_Count)) {
echo $ROW_LIKE['Likes'];
}
?>
</button>
<button id="Post-ImVi-Action-Share">815</button>
<div id="Post-ImVi-Action-Face"></div>
</div>
Here is php-SocialMedia-Posting-Like.php
<?php
include"php-MAIN-Info.php";
include"php-MAIN-SignUp+Database.php";
$Post = $_POST['Post_1'];
$Poster_Name = $_POST['Poster_Name'];
$Sql = "SELECT* FROM Likes WHERE Post = '$Post' AND Poster_Name = '$Poster_Name'";
$Like_Result = mysqli_query($conn, $Sql);
while ($Like_Row = mysqli_fetch_array($Like_Result)) {
$Like_Add = $Like_Row['Likes'] + '1';
$Sql_Like_Add = "UPDATE Likes SET Likes = '$Like_Add' WHERE Post = '$Post' AND Poster_Name = '$Poster_Name';";
$Like_Result_Add = mysqli_query($conn, $Sql_Like_Add);
if ($Like_Result_Add) {
echo $Like_Add;
}
}
update:
i found a error
php-Home-MSOHome.php:352 Uncaught ReferenceError: data is not defined
please try to help cause i'm new to this web.Thanks
why is no one answering my question you k i'm still looking for the answers
I did it its a php error udfinde Poster_name its Poster_Name

HTML AJAX not executing PHP

I am really new to AJAX/jQuery and PHP, and im trying to work on a little project that writes your daily weight to a Db which then is displayed later on with graphs etc.
I would like when the user submits the form for his or her weight that it displays a pop up message but for whatever reason, the AJAX/Jquery script doesn't seem to be doing anything with the php file therefore no info gets added into the database.
Here is the HTML Form: (index.html)
<form id="ajax-form" method="post" action="connection.php">
<div class="columns field">
<div class="column control is-half is-offset-one-quarter">
<input
class="input"
id="weight"
name="weight"
type="text"
placeholder="Enter your weight for the day"
/>
</div>
</div>
<div class="center-test">
<div class="field">
<div class="control">
<span class="select">
<select name="person">
<option value="Ayush">Ayush</option>
<option value="Sheri">Sheri</option>
</select>
</span>
</div>
</div>
</div>
<input type="date" name="weightdate" id="weightdate" />
<div class="field column is-half is-offset-one-quarter">
<button
type="submit"
id="submit"
name="submit"
class="button is-primary"
>
Submit
</button>
</div>
</form>
<div id="error_message" class="text-danger"></div>
<div id="success_message" class="text-success"></div>
AJAX/jQuery: (inside index.html )
$(document).ready(function () {
$("#submit").click(function (e) {
e.preventDefault();
var weight = $("#weight").val();
var person = $("#person").val(); // You miss this
var weightdate = $("#weightdate").val(); // You miss this
if (weight == "" || person == "" || weightdate == "") {
$("#error_message").html("All Fields are required");
} else {
$("#error_message").html("");
$.ajax({
url: "connection.php",
method: "POST",
data: {
weight: weight,
person: person, // Add this
weightdate: weightdate, // Add this
},
success: function (data) {
$("form").trigger("reset");
$("#success_message").fadeIn().html("data");
setTimeout(function () {
$("#success_message").fadeOut("Slow");
}, 2000);
},
});
}
});
});
PHP: (connection.php)
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
include_once 'dbconnect/db_info.php';
$weight = $_POST['weight'];
$person = $_POST['person'];
$date = $_POST['weightdate'];
$formatDate = date("d/m/y", strtotime($date));
//echo $formatDate;
if(date("m", strtotime($date)) == date("01")) {
$sql = "INSERT INTO WeightTracker (person, kg, weight_date, date_month) VALUES ('$person', '$weight', '$formatDate', 'January');";
#$result = mysqli_query($conn, $sql);
$result = mysqli_query($conn, $sql);
}
elseif(date("m", strtotime($date)) == date("04")) {
//echo working;
$sql = "INSERT INTO WeightTracker (person, kg, weight_date, date_month) VALUES ('$person', '$weight', '$formatDate', 'April');";
#$result = mysqli_query($conn, $sql);
$result = mysqli_query($conn, $sql);
}
else {
$sql = "INSERT INTO WeightTracker (person, kg, weight_date) VALUES ('$person', '$weight', '$date');";
#$result = mysqli_query($conn, $sql);
$result = mysqli_query($conn, $sql);
}
Does anyone have any ideas? When I remove the AJAX/jQuery code, the form successfully submits and the connection.php writes to the database with no issues.
Most of the problem was resolved in comments. The problem (as described in the comments) was a PHP error Undefined index on this line:
$person = $_POST['person'];
As I mentioned in an earlier comment: your person input is missing the expected person ID. That means this Javascript:
var person = $("#person").val();
Is actually undefined, so there is no person field POSTed to your PHP, so when you try to use it as $_POST['person'], you'll get an error.
To fix that, just add the id your Javascript is using to find the person:
<select name="person" id="person">
The data you give for the POST request is just weight, there is no person and weightdate data.
var weight = $("#weight").val();
var person = $("#person").val(); // You miss this
var weightdate = $("#weightdate").val(); // You miss this
if (weight == "" || person == "" || weightdate == "") {
$("#error_message").html("All Fields are required");
} else {
// Your code :)
}
And the data,
$.ajax({
url: "connection.php",
method: "POST",
data: {
weight: weight,
person: person, // Add this
weightdate: weightdate // Add this
},
success: function(data) {
$("form").trigger("reset");
$("#success_message")
.fadeIn()
.html("data");
setTimeout(function() {
$("#success_message").fadeOut("Slow");
}, 2000);
}
});
Your Problem lies here.
include_once 'dbconnect/db_info.php';
Change it to something like
realpath('__DIR__'.'/dbconnect/db_info.php');
Hopefully it will solve your problem.

Post a form array multiple times

I have an HTML form that contains a number of checkboxes. When the user clicks the submit button the checked array is posted and a query runs based on that result.
I have a button allowing the user to save their result, when they insert their saved result into another table.
But this means I need to post the form result again which it doesn't allow me to do. How would I fix this?
PHP Code
<?php
if (isset($_POST['submit']) && isset($_POST['sport'])) {
$class = $_POST["sport"];
foreach ($class As $key => $value) {
$query = "SELECT *
FROM sport b
join sport a
on a.Tag = b.Name
where a.SportID<> b.SportID and a.Tag = '$value'";
$result = mysqli_query($con, $query) or die("Invalid Query");
while ($row = mysqli_fetch_assoc($result)) {
echo "* $row[Name]\n";
}
}
} else if (isset($_POST['saved'])) {
$arr_class = $_POST["sport"];
foreach ($arr_class As $key => $newvalue) {
$query2 = "INSERT INTO save (Username, Name)
SELECT '$username', b.Name
FROM sport b
JOIN sport a
on a.Tag = b.Name
where a.SportID <> b.SportID
and a.Name = '$newvalue'";
$result2 = mysqli_query($con, $query2) or die('Result could not be saved!');
}
echo 'Result saved!';
}
JS Code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.submit-button').click(function (ev) {
var $item = $(this);
var formData = $('form').serialize();
$.ajax({
data: formData+'&submit=submit',
url:'test1.php',
type: "POST",
dataType: "html",
success:function (data) {
console.log(data);
if(confirm('You want to save \n'+data+ ' as your sport')){
$.ajax({
data: formData+'&saved=saved',
url:'test1.php',
type: "POST",
dataType: "html",
success:function (data) {
console.log(data);
}
});
}
}
});
});
});
</script>
HTML Code
<form id="form" action="" method="post">
<input type="checkbox" name="sport[]" value="Football">Football<br>
<input type="checkbox" name="sport[]" value="Rugby">Rugby<br>
<input type="checkbox" name="sport[]" value="Golf">Golf<br>
<input type="checkbox" name="sport[]" value="Basketball">Basketball<br>
<br> <input type="button" class="submit-button btn btn-info" name="submit" value="submit">
<input type="submit" style="display:none;">
</form>

Ajax won't post form data to PHP script

I got a form with some data that needs to be sent and I want to do it with ajax. I got a function that respond on an onclick event of a button. When I click the button I got some post data in firebug but it just doesn't reach my PHP script. Does anyone know what's wrong?
JS:
function newItem() {
var dataSet = $("#createItem :input").serialize();
confirm(dataSet); //Below this code box is the output of this variable to check whether it is filled or not
var request = $.ajax({
type: "POST",
url: "/earnings.php",
data: dataSet,
dataType: "json"
});
request.done(function(){
$('.create_item').children(".row").slideUp('100', 'swing');
$('.create_item').children("h2").slideUp('100', 'swing');
confirm("succes");
});
request.fail(function(jqXHR, textStatus) {
confirm( "Request failed:" + textStatus );
});
}
dataSet result when the form is completly filled in:
id=123&date=13-09-2013&amount=5&total=6%2C05&customer=HP&invoicenumber=0232159&quarter=1&description=Test
The PHP:
<?php
include('includes/dbconn.php');
function clean_up($string){
$html = htmlspecialchars($string);
$string = mysql_real_escape_string($html);
return $string;
}
if($_POST){
$date = clean_up($_POST['date']);
$amount = clean_up($_POST['amount']);
$total = clean_up($_POST['total']);
$customer = clean_up($_POST['customer']);
$invoicenumber = clean_up($_POST['invoicenumber']);
$quarter = clean_up($_POST['quarter']);
$description = clean_up($_POST['description']);
$sql = ("INSERT INTO earnings (e_date, e_amount, e_total, e_customer, e_invoicenumber, e_quarter, e_description)
VALUES ($date, '$amount', '$total', '$customer', $invoicenumber, $quarter, '$description')");
echo $sql;
if($mysqli->query($sql) === true){
echo("Successfully added");
}else{
echo "<br /> \n" . $mysqli->error;
}
}
?>
The form works fine without the ajax but with it it just doesn't work.
Your help is appreciated!
Try this snippet code bro...
<form id="F_login">
<input type="text" name="email" placeholder="Email">
<input type="password" name="password" placeholder="Password">
<button id="btn_login" type="submit">Login</button>
</form>
$("#btn_login").click(function(){
var parm = $("#F_login").serializeArray();
$.ajax({
type: 'POST',
url: '/earnings.php',
data: parm,
success: function (data,status,xhr) {
console.info("sukses");
},
error: function (error) {
console.info("Error post : "+error);
}
});
});
Reply me if you try this...
prevent your form submitting and use ajax like this:
<form id="createItem">
<input id="foo"/>
<input id="bar"/>
<input type="submit" value="New Item"/>
</form>
$('#createItem).on("submit",function(e){
e.preventDefault;
newItem();
});
Try this
<input type="text" id="foo"/>
<input type="text" id="bar"/>
<input type="button" id="btnSubmit" value="New Item"/>
<script type="text/javascript">
$(function() {
$("#btnSubmit").click(function(){
try
{
$.post("my php page address",
{
'foo':$("#foo").val().trim(),
'bar':$("#bar").val().trim()
}, function(data){
data=data.trim();
// alert(data);
// this data is data that the server sends back in case of ajax call you
//can send any type of data whether json or json array or any other type
//of data
});
}
catch(ex)
{
alert(ex);
}
});
});
</script>

json success function not executing

I am trying to post value of an input box and a obtain value of the checked radio button and perform a query based on which radio button is checked ... but my success function is not executing...
Html form :
<form class="form-inline" id="myForm">
<label class="radio">
<input type="radio" id="title1" name="title" value="title">
Title
</label>
<label class="radio">
<input type="radio" id="author" name="title" value="author">
Author
</label>
<label class="radio">
<input type="radio" id="subject" name="title" value="subject">
Subject
</label><br>
<input type="text" name="input"> </input>
<button class="btn btn-inverse" id="download" >Go</button>
</form>
jQuery :
$('document').ready(function(){
$('input[name=title]:first').attr('checked', true);
$('#download').click(function(){
value = $('input[name=title]:checked', '#myForm').val();
alert(value);
var input = $('#input').attr('value');
dataString = 'title='+ value +'&input='+input;
wurl = "downloadE.php";
$.ajax({url: wurl, type: "POST",dataType: "json",data:dataString ,success: function(data){
alert("success");
}
})
})
});
php code:
$value = $_POST['title'];
$output = $_POST['input'];
if($value=="title")
{
$query = " select * from library where Title = '$output'; ";
}
else if($value=="author")
{
$query = " select * from library where Author = '$output'; ";
}
else if($value=="subject")
{
$query = " select * from library where Subject = '$output'; ";
}
$result = mysql_query($query);
$ret = array();
while($info = mysql_fetch_array( $result )){
$ret[] = $info;
}
echo json_encode($ret);
When I encounter these types of issues, I'll add an error function and output the details to the console log so that it will help figure out the error. Like so:
,success: function(data){
alert("success");
},error: function(e){
console.log(e);
}
Try this
var callback = function( resp ) {
alert(123);
}
$.post(url, data, callback, "json");
<input type="text" name="input"> </input>
is wrong, and you're missing an ID I believe:
<input type="text" name="input" id="input" />
You also need to check that your script is executing. If nothing is being return in your JavaScript it means that the script is failing on the server side.

Categories