Calling a view through jquery and parsing html - php

I have a page in php home.php and it has views something like this
home.php
<?php
?>
<div id="showView">
Show User
Show Car
</div>
<div id="userView">
<p>This is view where user profile is shown</p>
</div>
<div id="carView">
<p>This is view where user car is shown</p>
</div>
<script>
$(document).ready(function()
{
$("#user").click(function(e)
{
//This where I am confused
var car = $("#carView").html();
$("#showView").append(car);
});
});
</script>
Now I am confused that how to pick userView and append to showView I tried a bit of coding but it didn't help and it didn't show result. Moreover is there any way that when that userView is appended then its html is shown when I view-source a page?

You have two options:
1) you generate the html on the fly using jquery (Not recommended)
<div id="showView">
Show User
Show Car
<div id="view_content"></div>
</div>
<script>
$(document).ready(function()
{
$('.view_trigger').click(function () {
var viewHTML = '';
if ($(this).attr('id') == 'user') {
viewHTML = '<div id="userView"><p>This is view where user profile is shown</p></div>';
} else if ($(this).attr('id') == 'car') {
viewHTML = '<div id="userView"><p>This is view where user car is shown</p></div>';
}
$("#view_content").html(viewHTML);
});
});
</script>
2) show/hide the container on trigger
<div id="showView">
Show User
Show Car
</div>
<div id="userView" class="view_content" style="display:none">
<p>This is view where user profile is shown</p>
</div>
<div id="carView" class="view_content" style="display:none">
<p>This is view where user car is shown</p>
</div>
<script>
$(document).ready(function()
{
$('.view_trigger').click(function () {
var viewHTML = '';
$('[class=view_content]').hide();
if ($(this).attr('id') == 'user') {
$('#userView').show();
} else if ($(this).attr('id') == 'car') {
$('#carView').show();
}
});
});
</script>
I don't recommend any of these solutions, you should use MVC for a better code.

Related

Show specific modal depending on PHP url variable

How I can show a specific modal depending on a variable in php which is modified in the url.
I'm using as responsive framework Bootstrap.
I have a page containing several modals, each corresponding to a product. I need to make a link from other parts of the site to go to page "products.php" and automatically display the required product.
E.g:
href="products.php?prod=1"
So far, I have only succeed in showing the modal when loading the page, but if I add or change the variable, it has no effect, not working at all.
This is what I have:
At the beginning
<?php
//Variable to choose which modal to show, default = 1
$prod = 1;
?>
Then the modal which has an id for identification.
<div class="modal fade" id="product1" tabindex="-1" role="dialog" aria-hidden="true">
<!-- Content of the modal -->
</div>
Then the script that show the modal when the page loads.
<script type="text/javascript">
$(document).ready(function(){
<?php if ($prod == 1) {
echo "$(document).ready(function () {var a = 'PD1'; alert(a); });";
echo "$('#product1').modal('show');";
}elseif ($prod == 2) {
echo "$(document).ready(function () {var a = 'PD2'; alert(a); });";
echo "$('#product2').modal('show');";
};
?>
});
</script>
Any idea?
You not gathering the value of the product variable from the URI. To accomplish this at the top of the page where you have $prod=1; you can put the following line of code :
$prod = (isset($_GET['prod']))?$_GET['prod']:"1";
This tells the system to get the value of the prod= in your url if it can't find it then use "1" as a default.
This is not the ideal answer, there are quite a few reasons not to access $_GET/$_POST variables in this way without proper filtering. I leave that for you to research.
You are doing opposite, should be
<?php if ($prod == "1") { ?>
<script type="text/javascript">
$(document).ready(function(){
var a = 'PD1';
alert(a);
$('#product1').modal('show');
});
</script>
<?php } elseif ($prod == "2") { ?>
<script type="text/javascript">
$(document).ready(function(){
var a = 'PD2';
alert(a);
$('#product2').modal('show');
});
</script>
<?php } ?>

i can't get the has-error text field in bootstrap

i've been trying to learn yii recently and so i started to work on an simple form with bootstrap. im using yii 1.1.x version not version 2 so i had to install bootstrap manually and write coding based on it.
anyway coming back to the problem it seems that my has-success class is being called correctly by jquery but it does'nt seem so for the has-error...
can someone help and im grateful for your help
<script>
$(document).ready(function() {
$('#contact_name').on('input', function() {
var input = $(this);
var is_name = input.val();
if (is_name) {
$('#contact_name').removeClass("has-success").addClass('has-error');
} else {
$('#contact_name').removeClass("has-error").addClass('has-success');
}
});
});
</script>
<h1>Example Form Validation</h1>
<br>
<div class="form " id="cla">
<?php $form=$this->beginWidget('CActiveForm',array( 'id' =>'form','htmlOptions'=> array('class'=> 'form-horizontal', 'name' => 'forvalidate', 'id' => 'registration-form' ))); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<div class="form-group col-lg-5" id='contact_name'>
<?php echo $form->label($model,'name',$htmloptions = array('class'=>'control-label ',)); ?>
<div class="controls">
<?php echo $form->textField($model,'name',array('name' =>'name' ,'class'=>'form-control col-xs-5', 'aria-describedby'=>'inputSuccess4Status')); ?>
</div>
</div>
</div>
<?php $this->endWidget(); ?>
</div>
<!-- form -->
Try
$('#contact_name').on('input', function() {
var input = $(this);
var is_name = input.val();
if (!is_name) {
$('#contact_name').parent().removeClass("has-success").addClass('has-error');
} else {
$('#contact_name').parent().removeClass("has-error").addClass('has-success');
}
});
I think "has-error" and "has-success" classes don't apply directly on the input but the div where the inputs are
You have to add these classes. See the highlighted code below:
$(document).ready(function() {
$('#contact_name').on('input', function() {
var is_name = $(this).val();
if (is_name) {
$('#contact_name').removeClass("has-success").addClass('has-error');
// ^^^^^^^^^^^^^^^^^^^^^^^
} else {
$('#contact_name').removeClass("has-error").addClass('has-success');
// ^^^^^^^^^^^^^^^^^^^^^^^^
}
});
});

Testing Captcha in php

I'm hoping of having this captcha on my website:
http://lirullu.com/
how would i test the outcome of this in a php form? Here is the code I have in my form.php file currently:
<script type="text/javascript">
$(window).load(function() {
var righthash1 = "cc03412089f87b9c509aaaa1";
$("#hf023d03").sortable({
items: ".cnt", stop: function () {
hash = "";
$("#hf023d03 .cnt").each(function (a, b) { hash += $(b).attr("id"); }); if (hash == righthash1) {
$(".slg1").fadeIn("fast");
} else {
$(".slg1").fadeOut("fast");
}
}
});
});
</script>
<div id="first_cnt">
<h2>Are you a traveline hero?</h2>
<span class="htext">(drag the answer)</span>
<div class="d1" id="hf023d03">
<div class="cnt" id="509aaaa1"><span class="yes">S</span></div>
<div class="cnt" id="89f87b9c"><span class="yes">E</span></div>
<div class="cnt" id="cc034120"><span class="yes">Y</span></div>
</div>
<div class="slg1">
<h3>is the correct answer!</h3>
</div>
</div>
I would seriously consider using Google's reCaptcha which has possibly the nicest API.
https://www.google.com/recaptcha
You can follow the tutorials and guides, it provides you the PHP and the Javascript you need to integrate the system with any website you create.

Load new page in div part 2

I have managed to load the new page into the div (thanks to everyone for your help) but it looks pretty bad (got menu bar and logo, but I only wanted the content), so instead I need to load only a div from the new page. I tried a new script but got redirected to the new page. Please help.
<script>
jQuery(document).ready(function() {
jQuery('.stil_link_img a').click(function(){
var x = $(this).attr("href") + "#content_eco";
jQuery("#new_page").load(x);
return false;
});
});
</script>
<div id="pachete">
<?php
$result=mysql_query("SELECT* FROM imagini");
while($data=mysql_fetch_row($result)){
if( ($data[3]==1)&&($data[2]==2) ){ ?>
<div class="stil_link_img">
<img src="upload/<?php echo $data[1];?>">
</div>
<?php }
}?>
</div>
<div id="new_page">
//some content which should be replaced with my loaded page
</div>
jQuery(document).ready(function() {
jQuery('.stil_link_img a').click(function(){
var $pageContent = jQuery('<div/>').load($(this).attr("href"));
jQuery("#new_page").html(jQuery("#content_eco",$pageContent).html());
return false;
});
});
I assume #content_eco is the divisions ID in the new page(the url from href attribute).
or you can load just the content from the url and avoid the link postback as
<script>
jQuery(document).ready(function() {
jQuery('.stil_link_img a').click(function(){
var x = $(this).attr("rel") + " #content_eco";
jQuery("#new_page").load(x);
return false;
});
});
</script>
<div id="pachete">
<?php
$result=mysql_query("SELECT* FROM imagini");
while($data=mysql_fetch_row($result)){
if( ($data[3]==1)&&($data[2]==2) ){ ?>
<div class="stil_link_img">
<img src="upload/<?php echo $data[1];?>">
</div>
<?php }
}?>
</div>
<div id="new_page">
//some content which should be replaced with my loaded page
</div>
Hope this helps you.

jquery function passsing variables

hi i am building a php mysql database pagination page, so i have a list of records 2 rows long at the bottom of the record i want a div which opens up when the span above it is clicked, how do i set up the jquery to make it so that it takes the id of the <p> and expands it in jquery
<span id="button1">Toggle</span>
<p id="p1">
hello<br/>hello
</p>
<script>
$("#button1").click(function () {
$("#p1").slideToggle("slow");
});
</script>
when i output it in php mysql the button will all have a different id and the p will have different ids as well
//Jquery Code which calls toggle_visibility function
$(".pOne").click(function(){
toggle_visibility('partsIdThree');
})
.toggle( function() {
$(this).children("span").text("[-]");
}, function() {
$(this).children("span").text("[+]");
});
//HTML Code
<div id="parts_toogle_one">
<p class="pOne">Add Records <span>[+]</span></p>
<div id="msg_body_one">
<tr><td></td></tr>
</div>
</div>
// Javascript code
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == "inline") {
e.style.display = 'none';
}
else if(e.style.display == "none") {
e.style.display = "inline";
}
}
Something like this..
You can use a dynamic id retrieving, so you don't have to worry about the number of results..
For example:
<?php $i = 0;?>
<?php foreach($resultset as $result) : ;?>
<span class="activator" id="result<?php echo $i;?>">CLICK HERE</span>
<div id="panel_result<?php echo $i;?>">SLIDER DIV</div>
<!-- Do the rest of you processing of $result here; it just doesn't matter, for here you only need some identification, I used a number for convenience. -->
<?php ++$i;?>
<?php endforeach;?>
Your jquery:
$('.activator').click(function(){
var the_id = $(this).attr('id');
var the_panel = $('#panel_' + the_id);
$(the_panel).slideToggle('slow');
});
So you don't have to write a jQuery command for each line you print in php, use just this snippet and it will work out by itself which panel to slide, according to which span is clicked.

Categories