How to load information to another page when button is clicked - javascript

When I click a button, I'm trying to get the name of the button to display on the next page. So far, I've managed to display the button name in an alert box but not sure how to get it to appear on another page.
<script>
$(document).ready(function(){
$("#test").click(function(){
alert("Text: " + $("#test").text());
});
});
</script>
The code below gets modules that the logged in user is registered to and displays two buttons. I'm trying to display the button label(subject names) on another page rather than in the alert box. So when I click on a button, the label name appears on the next page.
$sql = DB::getInstance()->get('modules', array('username','=' ,$user->data()->username));
if(!$sql->count()){
echo 'No data';
}else {
foreach ($sql->results() as $sql){ ?>
<p> <?php echo $sql->name; ?></p>

If you are trying to navigate to another page on click, you can just modify your jquery event handler to this and pull it out of the query params on the next page.
$(document).ready(function(){
$("#test").click(function(){
window.location = "your_next_page.php?name=" + $("#test").text();
});
});
on your other page.
<?php
echo $_REQUEST['name'];
?>

Save the $sql->name to session data and use this another page
$sql = DB::getInstance()->get('modules', array('username','=' ,$user->data()->username));
if(!$sql->count()){
echo 'No data';
}else {
foreach ($sql->results() as $sql){
$_SESSION['name'] = $sql->name;?>
<p> <?php echo $sql->name; ?></p>
another_page.php
session_start();
echo $_SESSION['name'];

Related

How to take the page number the user want for Turn.js

I'm using the turn.js to make a catalog. I grab the pages from the database with PHP and I want to make some control buttons like search specific page. How can I parse the value from the searchbox to take the place of number 10 here $("#flipbook").turn("page", 10); and run it?
<div class="flipbook-viewport">
<div class="container1">
<div class="flipbook">
<?php
if (isset($_GET['eb_catalogs_id'])){
$catalogos_id = $_GET['eb_catalogs_id'];
$query= "SELECT c_catalog_name FROM eb_catalogs WHERE c_id = $catalogos_id";
$test= $conn->query($query);
$catalogname =$test->fetchColumn();
$query1 = ("SELECT * FROM eb_catalog_".$catalogname."");
$pages = $conn->query($query1);
$i = 1;
foreach($pages as $page){
echo "<div class='p".$i."' style='background-image:url(https://untuneful-carload.000webhostapp.com/img/catalogs/".$catalogname."/".$page['eb_catalog_imgs'].")'></div>";
$i++;
}}else{
echo "nothing";
}
?>
</div>
</div>
</div>
<script type="text/javascript">
function loadApp() {
// Create the flipbook
$('.flipbook').turn({
width:922,
height:600,
elevation: 50,
});
}
// Load the HTML4 version if there's not CSS transform
yepnope({
test : Modernizr.csstransforms,
yep: ['js/turn.js'],
nope: ['js/turn.html4.min.js'],
both: ['css/basic.css'],
complete: loadApp
});
alert("The current page is: "+$("#flipbook").turn("page"));
</script>
here is the code of the page so far.. I know how to make an input field but I dont know how to parse the value of the input field to the function that jumps to the specific page the user wants.
If you pass the page number to next page, you may get it using $_post or $_get and execute it on page load:
<script>
$(window).on("load",function(){
$("#flipbook").turn("page", <?php echo ($_GET["page"]) ?>);
})
</script>
If you are in the same page and just want to flip the book, you can easily get the number using val():
<input id="pageNumber">
<button onclick="flipPage()">GO</button>
<script>
function flipPage(){
var pageNumber=$('#pageNumber').val();
$("#flipbook").turn("page", pageNumber);
}
</script>

Change of div in the same page without refresh

In one of my pages, I have an <a> tag. When I click it, I am passing the variable as a GET parameter and retrieving it in the same page and displaying the details.
The code to get the parameters:
if(isset($_GET['CatId']))
{
$CatId= $_GET['CatId'];
}
else $CatId=0;
if(isset($_GET['MainProductId']))
{
$MainProductId= $_GET['MainProductId'];
$FilterAllProductQuery ="WHERE Product.MainProductId = '$MainProductId'";
$FilterProductQuery = "AND Product.MainProductId = '$MainProductId'";
}
else
{
$MainProductId=0;
$FilterAllProductQuery="";
$FilterProductQuery="";
}
The <a> tag:
<a href='Products.php?CatId=<?php echo $CatId;?>&MainProductId=<?php echo $id;?>' ><?php echo $row["MainProdName"] ?></a>
The details to be displayed:
if($CatId == 0)
{$sql = "SELECT *,Product.Id AS ProdId, Product.Name as ProdName FROM Product $FilterAllProductQuery ";}
else
{$sql = "SELECT * ,Product.Id AS ProdId, Product.Name as ProdName FROM Product INNER JOIN MainProduct ON MainProduct.Id = Product.MainProductId
INNER JOIN Category ON Category.Id = MainProduct.CategoryId WHERE Category.Id = '$CatId' $FilterProductQuery ";}
$result1 = $dbcon->query($sql);
if ($result1->num_rows > 0) {
while ($row = $result1->fetch_assoc()) {
$id = $row["ProdId"];
// $image=$row["ImagePath1"];
$qty = $row["Quantity"];
?>
<li class="col-lg-4">
<div class="product-box">
<span class="sale_tag"></span>
<div class="row">
<img src='themes/images/<?php echo $row["ImagePath1"]; ?>' height='200' width='250'> </a></div></div></li>
Now the code is working fine, but what's happening is that when I click the <a> tag, as I am passing the get parameters, the page is refreshing. As all the code are on the same page, I don't want the page to be refreshed. For that, I need to use Ajax request. How can I do that?
I would make an onclick() event on the a tag like so:
<?php echo '<a c_id="'.$CatId.'" MainProductId="'.$id.'" onclick="sendProduct()">'.$row["MainProdName"].'</a>';
Afterwards i would in a .js file write a simple function called sendProduct() and inside i would do an ajax request to a page named ex: insertproduct.php, get the information back and Insertproduct.php would process the data and you could use .html() or .append() to assign the data to the div showing the data with a normal id.
The c_id, MainProductId are custom attributes you could recieve in the .js file as $("#youraTag").attr("c_id");
There's a really good guide here on basic ajax: https://www.youtube.com/watch?v=_XBeGcczFJo&list=PLQj6bHfDUS-b5EXUbHVQ21N_2Vli39w0k&index=3&t=1023s
First you have to remove the href of the link and give it an id.
<a id='link'>
<?php echo $row["MainProdName"] ?>
</a>
Then you put this jQuery into your page. Note, you need to have a div in which you are going to put in all your obtained results. I reffered to this div in the code as #mydiv.
$("#link").click(function(){
$("#mydiv").load("Products.php?CatId=<?php echo $CatId;?>&MainProductId=<?php echo $id;?>");
});

Javascript in php refresh page after dialog

Hello I have the below php code:
if (isset($_POST['prano'])) {
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('aplikacioni');
$mbajtesi = $_SESSION['mbajtes'];
for ($i=1; $i<=$mbajtesi; $i++) {
if (empty($notat[$i])) {
$emp = "emp";
}
}
if ($emp == "emp") {
echo ("<script language = 'javascript'> window.alert('Something!!!');window.location.href='Snotat.php';</script>");
exit();
}
}
Now, this code means: When I press the button named 'prano' then displayed a table with input values.The user must fulfill all values..If not then he gets a message...
So I don't want after the dialog message to disappear the table [remember the table appear when press button 'prano']...just I want to appear a dialog message without disappear the table..I've tried location.reload() but not work..any idea?
Your code is OK. Can you check your $_SESSION['mbajtes'] & confirm that, session_start(); line is exist in top of your page?
The problem is that you don't need to reload the page. You should use javascript/jQuery to insert an element to the body (for example) with that message.
Example:
<?php if ($emp == "emp") { ?>
<script>
jQuery(document).ready(function($){
$('body').prepend('<div>Your message Here</div>);
});
</script>
<?php } ?>
Remember to add jQuery to your header or footer.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

How can I pass the name of the field in form_error ()

It's part of my view. Me need transmit input name in on which i click.
Below is a script that will get the name input after click
<div class="form_hint"><?=form_error('this get value from javascript after click')?></div>
<?php echo form_open("onenews/" . $onenews['id'] . "#signup", $form_reg['main_form']); ?>
<?php echo form_input($form_reg['login'], $this->form_validation->set_value('login')) ?>
<?php echo form_input($form_reg['email'], $this->form_validation->set_value('email')) ?>
<?php echo form_input($form_reg['password']) ?>
<?php echo form_input($form_reg['conf_password']) ?>
<?= MY_Controller:: create_captcha(); ?>
<?php echo form_input($form_reg['captcha']) ?>
<?php echo form_input($form_reg['submit']) ?>
<?php echo form_close(); ?>
</div>
</div>
jq
<script type="text/javascript">
$(function(){
var curInput = '';
$('#form_reg').find('input').on('click', function(){
curInput = $(this).attr("name");
});
})
</script>
Or i must use ajax?
Thanks!
Your question is not clear at all, but I assume you want to dynamically change the content of the form_hint div. You can't do that with PHP. Once PHP renders the page, it shuts down and does nothing more. So the only way to make PHP show that message is after the form submit, but then you lose your click data. There is a way to save the clicked element in a session for example, but that would be a really bad solution.
So the best solution would probably be to list all the hints in a JavaScript variable, and call the appropriate one upon the click event, and fill the form_hint div with it.
$('.form_hint').text(yourAppropriateMessageHere);
An example with a hidden div for the login input field with exactly how you described would be:
<div class="form_text_login"><?=form_error('login')?></div>
JS
var loginMessage = $('.form_text_login').text();
// list others here
// then make a tree with switch/case or if/else
if (curInput == 'login') {
$('.form_hint').text(loginMessage);
}
else if (curInput == 'email') {
// do the same with the email message, etc.
}

deleting an alert in an "if" statement

I have an if statement that currently has an alert pop up when the next button on the page is clicked. Im not sure what i have to remove to just have the alert go away. I still need the button to take you to the next page. No matter what I adjust, the end result is the button no longer works. Here is the code:
$('.dab_button').click(function(){
if($('[name=select_value1]').val()=='' ||
$('[name=select_value2]').val()=='' ||
$('[name=select_value3]').val()==''){
alert('Please select your gift choices for each or the three levels.');
} else{
<? if($members_info['individual']=='no'):?>
window.location.href = "/dab/profile";
<? else:?>
window.location.href = "/hq/edit_page";
<? endif;?>
}
});
Just comment it out?
$('.dab_button').click(function(){
if($('[name=select_value1]').val()==""||$('[name=select_value2]').val()==""||$('[name=select_value3]').val()==""){
; // do nothing
// alert('Please select your gift choices for each or the three levels.');
} else{
<? if($members_info['individual']=='no'):?>
window.location.href = "/dab/profile";
<? else:?>
window.location.href = "/hq/edit_page";
<? endif;?>
}
});
But I stronly recommend to give some feedback to the user when the button does nothing (does not navigate away), because they have not filled out all inputs. If you wanted to navigate in any case, just remove that if-statement (note that you also have serverside processing code, which has nothing to do with the javascript):
$('.dab_button').click(function(){
<? if($members_info['individual']=='no'):?>
window.location.href = "/dab/profile";
<? else:?>
window.location.href = "/hq/edit_page";
<? endif;?>
});

Categories