Change PHP include ("link.php") by click buttons - php

how to change PHP include ("link.html") by click buttons.
<?php include('link1.html')?>
BUTTON 1 change <?php include('link1.html')?> to <?php include('link2.html')?>
BUTTON 2 change <?php include('link1.html')?> to <?php include('link3.html')?>
BUTTON 3 change <?php include('link1.html')?> to <?php include('link4.html')?>
how to do this without refreshing page. using ajax?

Wrap include('link1.html'); with a DIV with unique ID. And on click on button call a ajax and replace the DIV content.
Please try with below code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="DIVID">
<?php
include('link1.html');
?>
</div>
<button onclick="btnclick('link2.html')">Button 1</button>
<button onclick="btnclick('link3.html')">Button 2</button>
<button onclick="btnclick('link3.html')">Button 3</button>
<script type="text/javascript">
function btnclick(_url){
$.ajax({
url : _url,
type : 'post',
success: function(data) {
$('#DIVID').html(data);
},
error: function() {
$('#DIVID').text('An error occurred');
}
});
}
</script>

You can do that with jquery, example:
PHP Code:
<div id="mainDiv"><?php include('link1.html')?></div>
<button onclick="change2()">Button 1</button><br />
<button onclick="change3()">Button 2</button><br />
<button onclick="change4()">Button 3</button><br />
JQuery code
function change2() {
$('#mainDiv').load('link2.html');
}
function change3() {
$('#mainDiv').load('link3.html');
}
function change4() {
$('#mainDiv').load('link4.html');
}
Don't forget to include de JQuery
<script src="jquery/jquery.min.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="include( $( this ) )" value="2">Button 1</button>
<button onclick="include( $( this ) )" value="3">Button 2</button>
<button onclick="include( $( this ) )" value="4">Button 3</button>
<div id="included_page"><?php include('link1.html')?></div>
function include(elem)
{
var page = elem.val();
$.ajax({
url: "link" + page + ".html",
type: "GET"
}).done(function(msg) {
$('#included_page').html(msg);
})
})
}
I am not great at ajax but something along these lines may work, if you get any problems let us know.

Related

Simple PHP AJAX script

I have 2 scripts, submit.php and display.php.
I want to be able to load submit.php, click the submit button & the result div to display 123.
Right now, it just reloads my page. I'm not getting anything from my Console so stuck how to debug.
Can someone take a look and provide assistance?
submit.php:
<form>
<input type="submit" value="Submit" name="display" id="display">
</form>
<div id="result"> </div>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
$("#display").click(function() {
$.ajax({
url: 'display.php',
type: 'GET',
dataType: "html",
data: {
"id": "123",
},
success: function(data) {
//called when successful
$('#result').html(data);
},
error: function(e) {
//called when there is an error
//console.log(e.message);
}
});
});
</script>
display.php:
<?php
$id = $_GET['id'];
echo $id;
?>
It submits or reloads the page. You need to prevent the default action. Change the first two lines:
$("#display").click(function(e) { // Add e (event) parameter.
e.preventDefault(); // Prevent the default action for this event.
You may change to type="button", but not sure if it will be effective.
solved your issue please
replace
<input type="submit" value="Submit" name="display" id="display">
by
<button type="button" name="display" id="display">Submit</button>

How to hide a submit button in codeigniter

How to hide the submit button when $row['completion_status']=='completed'.
My view code is:
<input type="submit" <?=($row[ 'checklist_id']=="Denied" ||$row[ 'checklist_id']=="Approved" ||$row[ 'completion_status']=="completed" )? "disabled='true'": "";?>name="sumit_button"
value="Update"
class="btn"
style="float:left;background:#d8d8d8;color:#000;box-shadow:0px 0px 1x rgba(0,0,0,0.2)!important;">
try like this code
<?php
$display = ($row[ 'checklist_id']=="Denied" ||$row[ 'checklist_id']=="Approved" ||$row[ 'completion_status']=="completed" )? "display:none": "display:block";
?>
<input type="submit" name="sumit_button"
value="Update"
class="btn"
style="float:left;background:#d8d8d8;color:#000;box-shadow:0px 0px 1x rgba(0,0,0,0.2)!important;<?php echo $display; ?>">
Use a proper way to do it:
<script>
$('#YOUR SUBMIT ID').submit(function(){
//Ajax Data
$.ajax({
type:'POST',
url:'YOUR BACKEND.php',
data:'YOUR DATA',
success: function() {
//Hide Button
$('#YOUR SUBMIT ID').attr("disabled", true);
},
error: function() {
//YOUR Code
}
});
});
</script>
change your disable into hidden just make the output just like this
<input type="submit" name="submit" hidden />
Or make it jQuery in it since you put a tag of javascript in this question

ajax submit button not working

I want to display the comment content by fill in the password inputted in the comment form by the end-user but the ajax submit button is not working. Is their any conflict?
Please help me to improve this code.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function() {
var password = $("#search").val();
if (password != "") {
$("#result").html("<img alt="ajax search" src='ajax-loader.gif'/>");
$.ajax({
type: "post",
url: "search.php",
data: "password=" + password,
success: function(data){
$("#result").html(data);
$("#search").val("");
}
});
}
}
$("#button").click(function(){
search();
});
$('#search').keyup(function(e) {
if (e.keyCode == 13) {
search();
}
});
});
</script>
<body>
<?php if(!$userdata->data->ID):?>
<form method="post" action="">
<input type="password" id="search" placeholder="This is a secret question." />
<input type="button" id="button" value="Submit" />
<ul id="result"></ul>
</form>
<?php endif?>
</body>
the same problem but i have solution
it's weird a little bit but
the solution is to remove type="button"
and use type="submit"

how to 'off' all events with one call

Simply the question is that, how to off all events with one call, for example if I have another events like 'mouseleave, mouseenter, keyup, keydown ...'.
What I'm doing here is that each time the dialog is showed I turn off (off) the events, this 'off' works well with click, but I want a code to turn off all events with one call, I tryed: $('.dialog').off('**'); but it doesn't works. If I don't use off I get multiple calls to click (multiple hello worlds).
I have a code like this:
myform.php
<script type="text/javascript">
$(document).ready( function () {
$('.dialog').off('click');
$('.dialog').on('click', '.mybutton', function() {
alert('hello world');
});
});
</script>
<input type="button" class="mybutton" value="click me!"/>
html:
<html>
<head>
<script type="text/javascript">
$(document).ready( function () {
function openDialog()
{
$.post( '/myform.php', null, function (data) {
$('.dialog').html( data );
$('.dialog').show();
});
}
function closeDialog()
{
$('.dialog').hide();
$('.dialog').html('');
}
});
</script>
</head>
<body>
<div class="dialog" style="display:none">
</div>
<input type="button" onclick="openDialog();" value="show dialog!" />
<input type="button" onclick="closeDialog();" value="close dialog!" />
</body>
</html>
You can pass no arguments and it unbinds all of them.
$("element").off();
jsFiddle.
I think that the unbind() method would work here.
http://api.jquery.com/unbind/
"removes all previously attached event handlers

How do I prevent AJAX from sending me to the top of the page?

I have the following signup form near the bottom of a new website. As soon as the AJAX response loads, the page skips to the top of the page. As far as I can tell, I have included "return false" correctly. What am I missing? Thank you!
## index.php ##
<script type="text/javascript" src="mailing-list.js"></script>
<div class="signup container">
<form id="signup-form" action="<?=$_SERVER['PHP_SELF']; ?>" method="get">
<fieldset>
<legend><h2 style="align:center;">Enter Your Email Address</h2></legend>
<div class="row">
<div class="offset4 span3">
<input class="email" type="text" name="email" id="email" />
</div>
<div class="span1">
<input type="submit" name="submit" value="Join" class="btn btn-large btn-primary" />
</div>
</div>
<div id="response">
<? require_once('inc/store-address.php'); if($_GET['submit']){ echo storeAddress(); } ?>
</div>
</fieldset>
</form>
</div>
## and mailing-list.js ##
$(document).ready(function() {
$('#signup-form').submit(function() {
// update user interface
$('#response').html('Adding email address');
// Prepare query string and send AJAX request
$.ajax({
url: 'inc/store-address.php',
data: 'ajax=true&email=' + escape($('#email').val()),
success: function(msg) {
$('#response').html(msg);
}
});
return false;
});
});
$(document).ready(function() {
$('#signup-form').submit(function(e) {
e.preventDefault();
// update user interface
$('#response').html('Adding email address');
// Prepare query string and send AJAX request
$.ajax({
url: 'inc/store-address.php',
data: 'ajax=true&email=' + escape($('#email').val()),
success: function(msg) {
$('#response').html(msg);
}
});
return false;
});
});
I doubt it is executing the default form submit behavior. Prevent it byt using the preventDefault method.
$(function(){
$('#signup-form').submit(function(e) {
e.preventDefault();
//your existing code goes here
});
});
You need to prevent the form from submitting the data (even if it is to the same file). Otherwise the page will reload before your ajax call is done. You do this with .preventDefault();
Try
...
$('#signup-form').submit(function(event) {
event.preventDefault();
// update user interface
...

Categories