Jump to content

codeigniter with jquery

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Hi all,i got codes in CI views like this

admin/product_home.php

<h1><?php echo $title;?></h1>

<p><?php echo anchor("admin/produk/create", "Create new product");?> | <?php echo anchor("admin/produk/export","Export");?></p> 


<?php

echo form_open_multipart("admin/products/import");

$data = array('name' => 'csvfile', 'size'=>15);

echo form_upload($data);

echo form_hidden('csvinit',true);

echo form_submit('submit','IMPORT');

echo form_close();

?>


<?php

if ($this->session->flashdata('message')){

	echo "<div class='message'>".$this->session->flashdata('message')."</div>";

}


if (count($products)){

	echo form_open("admin/products/batchmode");

	echo "<p>Category: ". form_dropdown('category_id',$categories);

	echo " ";

	echo "</p>";

	echo "<table border='1' cellspacing='0' cellpadding='3' width='500'>\n";

	echo "<tr valign='top'>\n";

	echo "<th> </th><th>ID</th>\n<th>Nama</th><th>Status</th><th>Actions</th>\n";

	echo "</tr>\n";

	foreach ($products as $key => $list){

		echo "<tr valign='top'>\n";

		echo "<td align='center'>".form_checkbox('p_id[]',$list['id'],FALSE)."</td>";

		echo "<td>".$list['id']."</td>\n";

		echo "<td>".$list['nama']."</td>\n";

		echo "<td>".$list['status']."</td>\n";

		echo "<td align='center'>".$list['status']."</td>\n";

		echo "<td align='center'>";

		//echo anchor('admin/products/edit/'.$list['id'],'edit');

		echo anchor("admin/produk/edit/".$list['id'],img(base_url().'/images/edit.jpg'));

       	echo " | ";

		//echo anchor('admin/products/delete/'.$list['id'],'delete');

		echo anchor("admin/produk/edit/".$list['id'],img(base_url().'/images/edit.jpg'));

       	

		echo "</td>\n";

		echo "</tr>\n";

	}

	echo "</table>";

	echo form_close();

}

?>


admin/product_create.php

<h1><?php echo $title;?></h1>

<?php

echo form_open_multipart('admin/produk/create');

echo "<p><label for='parent'>Category</label><br/>";

echo form_dropdown('cat_id',$categories) ."</p>";


echo "<p><label for='name'>Name</label><br/>";

$data = array('name'=>'nama','id'=>'nama','size'=>25);

echo form_input($data) ."</p>";


echo "<p><label for='short'>Short Description</label><br/>";

$data = array('name'=>'shortdesc','id'=>'short','size'=>40);

echo form_input($data) ."</p>";


echo "<p><label for='long'>Long Description</label><br/>";

$data = array('name'=>'longdesc','id'=>'long','rows'=>5, 'cols'=>'40');

echo form_textarea($data) ."</p>";


echo "<p><label for='uimage'>Upload Image</label><br/>";

$data = array('name'=>'image','id'=>'uimage');

echo form_upload($data) ."</p>";


echo "<p><label for='uthumb'>Upload Thumbnail</label><br/>";

$data = array('name'=>'thumbnail','id'=>'uthumb');

echo form_upload($data) ."</p>";


echo "<p><label for='harga'>Harga</label><br/>";

$data = array('name'=>'harga','id'=>'harga','size'=>10);

echo form_input($data) ."</p>";


echo "<p><label for='diskon'>diskon(%)</label><br/>";

$data = array('name'=>'diskon','id'=>'diskon','size'=>10);

echo form_input($data) ."</p>";


echo "<p><label for='harga_diskon' >Harga Diskon</label><br/>";

$data = array('name'=>'harga_diskon','id'=>'harga_diskon','size'=>10);

echo form_input($harga_diskon) ."</p>";


echo "<p><label for='status'>Status</label><br/>";

$options = array('active' => 'active', 'inactive' => 'inactive');

echo form_dropdown('status',$options) ."</p>";


echo "<p><label for='featured'>Featured</label><br/>";

$options = array('Y' => 'Y', 'N' => 'N');

echo form_dropdown('featured',$options) ."</p>";


echo "<p><label for='new'>New</label><br/>";

$options = array('Y' => 'Y', 'N' => 'N');

echo form_dropdown('baru',$options) ."</p>";


echo "<p><label for='promo'>Promo</label><br/>";

$options = array('Y' => 'Y', 'N' => 'N');

echo form_dropdown('promosi',$options) ."</p>";


echo form_submit('submit','create product');

echo form_close();

?>


and the Produk controller


 function index()

  {

	$data['title'] = "Manage Products";

	$data['main'] = 'admin/product_home';

	$data['products'] = $this->MProduk->getAllProducts();

	$data['categories'] = $this->MCat->getCategoriesDropDown();

	$this->load->vars($data);

	$this->load->view('dashboard');

  }

  

  function create(){

   	if ($this->input->post('nama')){

	   	$this->MProduk->addProduct();

  		$this->session->set_flashdata('message','Product created');

  		redirect('admin/produk/index','refresh');

  	}else

	{

		$data['title'] = "Create Product";

		$data['main'] = 'admin/product_create';

		$data['categories'] = $this->MCat->getCategoriesDropDown();

		$this->load->vars($data);

		$this->load->view('dashboard');    

	} 

  }



I'm intending to automatically fill the harga_diskon field with the calculated value from harga-diskon/100.

This is js script


<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.js"></script>

<script type="text/javascript">

$(document).ready(function(){

   

   $("#diskon").change(function()

   {

       var price = parseInt($("#harga").val());

       var discount = parseInt($("#diskon").val());


       var discounted_price = harga * (diskon/100);

       

       //alert(price + " + " + discount + " = " + discounted_price);

       $("#harga_diskon").val(discounted_price);

       return false;

   });

});

</script>


I try to put the js on the view page,it doesn't give me the expected results.Can someone tell me how to properly do it?And where should i put the js scripts?Thanks a lot...

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
JavaScript can go anywhere on your page but is most commonly found between your <head></head> tags. I don't see those in your PHP/HTML. Are you including a header or footer file somewhere?

Also, what does form_input() do? Does it just produce a text based input?

#3
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Yes Jordan,i'm using a header file. form_input() is the same as text based input..

#4
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
By the way,i got it worked out already..Thanks Jordan..