AJAX open link without page reload and PHP get variable - php

page.php
<script>
$(document).ready(function(){
// Set trigger and container variables
var trigger = $('.a'),
container = $('#content');
// Fire on click
trigger.on('click', function(){
// Set $this for re-use. Set target from data attribute
var $this = $(this),
target = $this.data('target');
// Load target page into container
container.load(target + ".php?id=<?php echo $_POST['id']?>");
// Stop normal link behavior
return false;
});
});
</script>
<nav id="nav">
<ul>
<form method="post">
<input name="id" value="12">
</form>
<li><a id="home" class="a" href="#" data-target="home">Home</a></li>
<li><a id="about" class="a" href="#" data-target="about">About</a></li>
</ul>
</nav>
<div id="content">
<style>
#home{display:none;}
</style>
<h1>Home</h1>
<p>This is the home page</p>
<h3>
</h3>
</div>
about.php
<h1>about</h1>
home.php
<h1>home</h1>
Script AJAX work good , without load it can open content infinity but problem is Row's number.
PHP
When i try to send ID on other content by POST method, or something it will not work.

Your question is not clear. Assuming you are trying to send the "id" as a get parameter via AJAX and loading content in some div/section.
$_POST should be replaced with $_REQUEST ideally and surrounded by "isset".
put an action on the form. put name and ID on the form values. Such as
<input name="id" name="id" value="12">

Related

How do I change a CSS Style attribute once a condition is met? PHP, JS, CSS

Good Day!
I am having a difficulty when it comes to showing a pop-up box in my webpage. I would like to show it when it meets a certain condition inside my php code, which is under the Condition.php. I have included the js file, which removes a certain class to make the box visible. How would I execute the JS code inside the Condition.php when it meets a certain condition?
Here are my codes:
Condition.php
<?php
// Defined variables and additional codes section
if (strlen($str) == 4) {
// Show the popup box
}
// Additional Codes
?>
ConfirmCheck.js
$(document).ready(function () {
$('#confirm').click(function () {
$('.popup').removeClass("hide");
});
});
Check.php
<form class="frm" action="Condition.php" method="POST">
// Additional Codes here
<input type="submit" name="checkOutBtn" value="CONFIRM" id="confirm">
</form>
<?php include 'box.php';?>
<script src='ConfirmCheck.js'></script>
Box.php
<div class="popup hide" id="popupID">
<div class="box">
<div class="form">
<h1>SUCCESS!</h1>
<form action="home.php">
<div class="form-group">
<p class="paragraph">
Your order has been successfully placed!
</p>
<button class="homepageBtn" onclick="home.php">GO TO THE HOME PAGE</button>
</div>
</form>
</div>
</div>
</div>
To do what you require simply put the if condition inside box.php and remove condition.php as it serves no purpose having an entire PHP page for a single condition.
box.php
<div class="popup <? if (strlen($str) != 4) { ?>hide<? } ?>" id="popupID">
<div class="box">
<div class="form">
<h1>SUCCESS!</h1>
<form action="home.php">
<div class="form-group">
<p class="paragraph">
Your order has been successfully placed!
</p>
<button class="homepageBtn" onclick="home.php">GO TO THE HOME PAGE</button>
</div>
</form>
</div>
</div>
</div>
I guess the problem is that you've set the action of your form to Condition.php but included the box design and code on check.php.
Note that #confirm is and input of type submit so after its pressed it will redirect you to the page specified at the action of the form.
I can suggest two possible fixes to that:
[least suggested] display the confirmation box on the Condition.php page
[most suggested] use AJAX!
The first fix requires you to move the markup and styles for box to the Condition.php file and design a whole confirmation/post action page
The second fix is better because by sending the Data to the server using AJAX you're not only going to stay on the same page (check.php) but you can also sort of hide the address to Condition.php which is supposed to be a backend file(from what i understood)
The structure should look something like this:
check.php:
<div class="frm">
// Additional Codes here
<buttin name="checkOutBtn" id="confirm">CONFIRMM</button>
</form>
<?php include 'box.php';?>
<script src='ConfirmCheck.js'></script>
ConfirmCheck.js:
$(document).ready(function () {
$('#confirm').click(function () {
// code to get all of your fields and put them in a js object: FDATA
$.ajax({type:'POST', url:'Condition.php', data: FDATA,
success:function(){
$('.popup').removeClass("hide");
}});
});
});
Condition.php:
<?php
// Defined variables and additional codes section
if (strlen($str) == 4) { //success
echo "success message";
}else{ // failed
header('HTTP/1.0 400 Bad Request');
die("bad request");
}
// Additional Codes
?>
The request goes back and forth between check.php and Condition.php in the background and your code gets notified through a callback whether or not to show the box.

How Bootstrap modal get 'id' from url

I want to get 'id' from url and using in my Bootstrap modal.
How to get this '69'.In my modal i using this '69' with SQL request.
I Hope, your JS code is somehow similar to it.
<script>
$('.Contact').click(function(){
var id=$('.Contact').val();
$.ajax({url:"ModalPage.php?id="+id,cache:false,success:function(result){
$(".modal-content").html(result);
}});
});
</script>
In, ModalPage.php
<?
$id=$_GET['id'];
// Now, use this 'id'.
?>
an easy trick is to make hidden input inside modal code as below
<div id="myModal">
<form>
<input type="hidden" value="<?php echo isset($_GET['id']?$_GET['id']:''); ?>" /> <!--if you want to make it hidden-->
<label>ID = <?php echo isset($_GET['id']?$_GET['id']:''); ?></label><!--if you want to display-->
<!--your code-->
</form>
</div>

PHP post jQueryUI menu selection

I have a jQuery UI menu inside a form and I would like to be able to pass the tag attribute as value for my POST in PHP.
Does anybody know how I can do that? I only know how to get that value via JavaScript.
Example:
<form id="cities" name="cities">
<ul id="menu" style="position:absolute;">
<li>Lodon</li>
<li>Madrid</li>
<li>Lyon</li>
<li>Paris</li>
</ul>
... other input fields...
... a submit button
</form>
I want to POST/GET the tag to the next PHP page.
<?php
// How can I get the tag or just the menu selection value (Lyon, Paris, etc)
and save it to a variable in PHP?
// I'm more interested in the tag
?>
Thanks in advance for your help,
Cristina
DEMO
JS code:
$(function() {
$( "#menu" ).menu({
select:function(event, ui){
console.log(ui.item);
alert("Selected tag = "+$(ui.item).find('a').attr('tag'));
$('#tag').val($(ui.item).find('a').attr('tag'));
}
});
});
HTML:
<form id="cities" name="cities">
<ul id="menu" >
<li>Lodon</li>
<li>Madrid</li>
<li>Lyon</li>
<li>Paris</li>
</ul>
<br style="clear:both;">
Selected menu tag (hidden field):
<input type="text" id="tag" name="tag">
</form>

Bootstrap Wizard Plugin with a PHP multi-page form?

I have a PHP form that is split into 3 separate PHP pages. The first page submits the form info which is then processed by a third party API. The returned (validated) results are then presented in the second PHP page along with a form for further processing.
My question is whether or not there is a way I can integrate this process into the BootStrap Wizard Plugin with its tab structure.
e.g. Fill and submit form by clicking on 'next' and have the Wizard plugin move to tab 2. showing the the next PHP page which has been processed.
<!-- Wizard -->
<section class="wizard">
<!-- Wizard navigation -->
<ul>
<li>Registration</li>
<li>Next step</li>
<li>Confirmation</li>
</ul>
<!-- Wizard progress bar -->
<div class="progress progress-line progress-striped">
<div class="progress-bar progress-bar-success"></div>
</div>
<!-- Wizard content -->
<div class="tab-content">
<div class="tab-pane" id="step1">
<h3>This is first step</h3>
<form method="post" action="myform.php?api=form&step=1">
<input type="text" name="phonenumber" id="phonenumber" />
<input type="submit" value="continue" class="btn btn-primary" />
</form>
</div>
<div class="tab-pane" id="step2">
<h3>Second step content</h3>
<form method="post" action="myform.php?api=form&step=2">
<input type="text" name="faxnumber" id="faxnumber" />
<input type="submit" value="continue" class="btn btn-primary" />
</div>
<div class="tab-pane" id="step3">
<h3>This is third final step (completed)</h3>
<p>Form Process Complete</p>
</div>
<!-- Wizard pager -->
<ul class="wizard pager">
<li class="previous">Previous</li>
<li class="next">Next</li>
<li class="next finish">Finish</li>
</ul>
</div>
</section>
<!-- /Wizard -->
<script src="/js/jquery.bootstrap.wizard.js"></script>
<script>
$(document).ready(function() {
$('.wizard').bootstrapWizard({onTabShow: function(tab, navigation, index) {
var wizard = $('.wizard');
var $total = navigation.find('li').length;
var $current = index+1;
var $percent = ($current/$total) * 100;
wizard.find('.progress-bar').css({width:$percent+'%'});
// If it's the last tab then hide the next button and show the finish instead
if($current >= $total && $total != 0) {
wizard.find('.pager .next').hide();
wizard.find('.pager .finish').show().removeClass('disabled');
} else {
wizard.find('.pager .next').show();
wizard.find('.pager .finish').hide();
}
}});
});
</script>
So what I'm trying to achieve is for the first form to show it's results in the second tab of the wizard.
Yes you can, I would suggest using the bwizard.js and bwizard.css plugin. And add a button to a form, now u going to have to call the action using ajax and not using an actual input of type submit. Using jQuery, on success of "btn_submit" you will taget the 2nd tab to focus to by giving each tab an I'd, or u can use page inspector and target the default I'd of each tab, which if I remember correctly used to be #step1 etc.

PHP Pass variable to popup form within same page

Following on from a previous question, (previous question here), the problem I'm having seems to involve trying to pass/post a value through a form when the form action is '#'. I've tried session data but it always returns the last item from the database. Everthing else returns nothing.
Any help/ideas/advice greatly received, S. (Code below)
This is the code that displays the list of items, each containing an 'email' link/button to one instance of a popup window/form that is located at the bottom of the page.
<?php
$query = mysql_query("select * from istable where categoryID = '1'");
while ($result = mysql_fetch_array($query)) {
echo '<h4>'.$result['title'].'</h4>
<p>'.substr($result['descrip'],0,408).'... <strong>Read more</strong></p>
<form action="#" method="post" rel="#sheet" class="see">
<input type="hidden" name="propTitle" value="'.$propResult['title'].'">
<input type="submit" name="submit" value="Email">
</form>
';
}
?>
This is the code for the popup window/form at the bottom of the same page that is called through jquery.
<div id="sheet" class="rounded">
<!--{{{ pane1 -->
<div class="pane" id="pane1">
<h4>Email Details to a Friend</h4>
<p>You have selected to forward the details of <?php echo $_POST['propTitle']; ?> to a friend.</p>
<p>Please fill out the following form</p>
<form class="rounded" id="email-form" method="post" action="<?php echo $pageLink; ?>">
<!-- form goes in here -->
</form>
</div>
<!--}}}-->
</div>
<script type="text/javascript">
$(".see").overlay({mask: '#999', fixed: false}).bind("onBeforeClose", function(e) {
$(".error").hide();
});
</script>
Why are you using PHP for this? If the popup is called through the same page, use JavaScript to get the DOM element value and if you need to process data use AJAX.

Categories