How to post the data in php using ajax??

Hey guys,

Once i got a situation to use iframe where i need to post the data of an employee in the same page using iframe(display all details of that employee). I couldn’t understand how to proceed..but i came to know that using ajax we can do that..

here is the scenario:

I need to click on a employee so that an iframe will open with his details..if i click another employee name then second employee details should be displayed in same iframe.

echo”<a href=’javascript:void(0);’ role=’button’ ¬†class=’empclick’ alt='”.$role[$i].”‘ onclick=’getElementValue(“.$role[$i].”,”.$Empid[$i].”,”. $status[$i].”)’>”;/*echo $role[$i];*/ echo $name[$i]; echo”</a>”;

Function:

function getElementValue(role,id,status)
{
//alert(role);

$.ajax({
type: “POST”,
url: “http://localhost:82/projectname/controllername/AssiningSession/”+ role+”/”+id+”/”+status ,
data: { value: role }
}).done(function( msg ) {
// alert( “Data Saved: ” + msg );
$(“#empcont”).attr(“src”,”iframeperffact”);
});
}

This helped me alot..hope it helps you too ūüôā

What’s the difference between POST and raw POST in PHP

Before going to description..first just check this link at glance

We can divide form submissions in three cases:

  1. Submissions with content type application/x-www-form-urlencoded
  2. Submissions with content type multipart/form-data
  3. Other submissions.

In cases 1 and 3, $HTTP_RAW_POST_DATA contains the raw post data (except if the option isalways_populate_raw_post_data is set to false, in which case $HTTP_RAW_POST_DATA is empty in case 1), i.e., the data exactly as the client (usually the browser) has sent it. In case, 1, the data has a form such as

key1=value1&key2=value2&key3[]=value3.1&key3[]=value3.2

PHP automatically parses this, so that $_POST becomes:

$_POST = array(
    "key1" => "value1",
    "key2" => "value2",
    "key3" => array("value3.1", "value3.2");
)

The contents of the raw data can also be access through php://input, even in case 1 whenalways_populate_raw_post_data is set to false. In particular,file_get_contents("php://input") gives the same data $HTTP_RAW_POST_DATA has or would have.

In case 3, in which the POST data is arbitrary, $_POST will be an empty array and$HTTP_RAW_POST_DATA will always be populated.

Case 2 is a special one. In that case, PHP will parse the data and $_POST will get the content of the fields which are not uploaded files, but php://input and $HTTP_RAW_POST_DATA will be unavailable.

To get the Raw Post Data:

<?php $postdata¬†=¬†file_get_contents(“php://input”);¬†?>

Split a string like 2|3 to 2 and 3 separately

Lets consider a table name called sample1 in our database.

in that table we have a field called designation which stored value like 1|3 and 2|3 etc like that..here one value defines designation and another one as role..say 1 as designation and 3 as role..so next designation also belongs to same role..

now we need split these values in to two and store it in different fields of a table..

Eg: SELECT designation FROM sample1;

output: split

Now we need to split these values to two different fields and store separately like below image:

splitted

 

To get in above format..use this code..

SELECT SUBSTRING_INDEX(designation,’|’,1) AS degID ,SUBSTRING(designation,LOCATE(‘|’,designation)+1, LENGTH(designation) ) AS RoleID ¬†FROM sample1

Very simple..right ūüėČ

Mysql split string in query

I would like to share, How to split string in MySQL query. While we design poor database design, we are forced to construct complex MySQL queries. Luckily MySQL has wonderful string functions. So we are going to use few of those functions to solve the issue.

Task :¬†¬†Split ‚Äú1000_33‚ÄĚ string by underscore ( _ ) in mysql query and get first part of string.

We usually do this in php using explode function like below.

1
2
3
4
<?php
$arr = explode("_","1000_33"); // splitting string by underscore( _ ).
$arr[0]; // getting first part of string
?>

The same thing we are going do in Mysql Query here.

Solution :

1
2
3
4
5
SELECT CASE LOCATE( '_', '1000_33' )
WHEN '0'
THEN '1000_33'
ELSE SUBSTRING_INDEX( '1000_33', '_', 1 )
END AS first_part_string

Explanation :

LOCATE function is used to figure out is the string has underscore ( _ ) or not. Suppose string doesn’t has underscore, It will return 0 else it will return position of underscore.
So,if string doesn’t have underscore means it will the query will return full string.

SUBSTRING_INDEX is used to split string. LOCATE function ensures string has underscore. So  SUBSTRING_INDEX function will split the first part of string

SUBSTRING_INDEX( ¬†‚Äô1000_33‚Ä≤, ¬†‚Äô_’, 1 )

 

  • Parameter 1 : String
  • Parameter 2 : Search String (Underscore)
  • Parameter 3 : Position of String

Hope this helps you ūüôā

How to create a stored procedure in MySQL

Introduction to SQL Stored Procedures

Summary: in this tutorial, you will learn what a stored procedure is, its advantages and disadvantages.

Definition of stored procedure

A stored procedure is a segment of declarative SQL code, which is stored in the database catalog. A stored procedure can be invoked by a program, a trigger or even another stored procedure.

A stored procedure which calls itself is known as recursive stored procedure. Almost RDMBS supports recursive stored procedure but MySQL does not support it very well. You should check your version of MySQL database before implementing recursive stored procedures.

Stored Procedure in MySQL

MySQL is known as the most popular open source RDBMS which is widely used by both community and enterprise. However during the first decade of its existence, it did not support stored procedure, trigger, event…etc. Since MySQL version 5.0, those features have been added to MySQL database engine to make it become flexible and powerful.

Before starting the tutorial series about stored procedure, you should have MySQL version 5.x+ installed in your computer or server.

Stored Procedures Advantages

  • Stored procedures help increase the performance of application using them. Once created, stored procedures are compiled and stored in the database catalog. A stored procedure typically runs faster than uncompiled SQL statements that are sent from external applications.
  • Stored procedures reduce the traffic between application and database server because instead of sending multiple uncompiled lengthy SQL statements, the application only has to send the stored procedure‚Äôs name with parameters.
  • Stored procedures are reusable and transparent to any applications. Stored procedures expose the database interface to all applications so developers don‚Äôt have to develop functions that are already supported in stored procedure in those applications.
  • Stored procedures are secured. Database administrator can grant appropriate permission to application that access stored procedures in database catalog without giving any permission on the underlying database tables.

Besides those advantages, stored procedures have their own disadvantages, which you should be aware of before deciding using store procedures.

Stored Procedures Disadvantages

  • Stored procedures make the database server high load in both memory and processors. Instead of being focused on the storing and retrieving data, you could be asking the database server to perform a number of logical operations which is not well-designed for database server.
  • Stored procedures consist of declarative SQL so it is very difficult to write a procedure with complexity of business logic like other languages in application layer such as Java, C#, C++‚Ķ
  • Stored procedures are difficult to debug. Only few database mangement system supports debugging stored procedures. There are some workarounds on this problem but it still not easy enough.
  • Store procedures are not easy to develop and maintain. Developing and maintaining stored procedure are usually required specialized skill set that not all application developers possess. This may introduce problems in both application development and maintain phases.

Stored procedures have their own advantages and disadvantages. When you develop applications, you should decide whether to use stored procedure or not based on the requirements. In the following tutorials, we will show you how to leverage stored procedures in your database programming tasks with a couple of practical examples.

Getting Started with MySQL Stored Procedures

Summary: in this tutorial, you will learn how to write the first MySQL stored procedure using CREATE PROCEDURE statement. We will show you how to call a store procedure using CALL statement.

Writing the first MySQL stored procedure

We are going to develop a simple stored procedure named GetAllProducts() to help you get familiar with syntax. The function of this stored procedure is to retrieve all products from the products table.

First launch the mysql client tool and type the following commands:

12

3

4

5

6

DELIMITER //CREATE PROCEDURE GetAllProducts()

BEGIN

SELECT *  FROM products;

END //

DELIMITER ;

mysql-stored-procedure-command-linepic

Let’s examine the stored procedure in a greater detail:

  • The first command is DELIMITER //, which is not related to the stored procedure syntax. The DELIMITER statement changes the standard delimiter which is semicolon (;) to another. In this case, the delimiter is changed from the semicolon(;) to double-slashes //. Why do we have to change the delimiter? because we want to pass the¬† stored procedure to the server as a whole instead of lettingmysql¬†interprets each statement at a time when we type.¬† Following the END keyword, we use delimiter // to indicate the end of the stored procedure. The last command (DELIMITER;)¬†changes the delimiter back to the semicolon (;).
  • The CREATE PROCEDURE statement is used to create a new stored procedure. You can specify the name of stored procedure after the CREATE PROCEDURE statement. In this case, the name of the stored procedure is¬†GetAllProducts.¬†Do not forget the parenthesis after the name of the store procedure or you will get an error message.
  • Everything inside a pair of keyword BEGIN and END is called stored procedure‚Äôs body. You can put the declarative SQL code inside the stored procedure‚Äôs body to handle business logic. In the store procedure we used simple SQL SELECT statement to query data from the¬†products¬†table.

It’s kind of tedious to write the store procedure in mysql client tool especially when the stored procedure is complex. Most of the GUI tool for MySQL allows you to create new stored procedures using intuitive interface. For example, in MySQL Workbench, you can create a new stored procedure as follows:

create-mysql-stored-procedure-mysql-workbench-step-1

 

create-mysql-stored-procedure-mysql-workbench-step-2

 

Enter the stored procedure code and click the Apply button

create-mysql-stored-procedure-mysql-workbench-step-3

You can review the code before MySQL stores it in the database. Click Apply button if everything is good.

create-mysql-stored-procedure-mysql-workbench-step-4

MySQL compiles and puts the stored procedure in the database catalog; click the Finish button.

create-mysql-stored-procedure-mysql-workbench-step-5

You can see a new stored procedure created under Routines of the classicmodelsdatabase

We have created a new stored procedure. Now it’s time to learn how to call or invoke it.

Calling stored procedures

In order to invoke a stored procedure, you use the following SQL command:

?

1 CALL STORED_PROCEDURE_NAME()

You use CALL statement to call a stored procedure e.g., to invoke the GetAllProducts stored procedure, you use the following command:

?

1 CALL GetAllProducts();

If you execute the command above, you will get all products in the products table.

mysql-stored-procedure

 

In this tutorial, you have learned how to write a simple stored procedure using CREATE PROCEDURE statement and invoke it using the CALL statement.

References: http://www.mysqltutorial.org/mysql-stored-procedure-tutorial.aspx

http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/

http://www.brainbell.com/tutorials/MySQL/Using_Stored_Procedures.htm

http://mysqlstoredprocedure.com/index.php?option=com_content&view=article&id=60:wrapper-procedure-to-load-specific-data&catid=34:select&Itemid=27

 

Code Igniter Error: Disallowed Key Characters

1. Search for function _clean_input_keys on /system/core/Input.php

2. Modify this line

exit(‚ÄėDisallowed Key Characters.‚Äô);

to

exit(‚ÄėDisallowed Key Characters.‚Äô . ¬†$str);

3. Refresh page to see the characters which generate the error

4. If you need to add those characters into the exception list, just add to this line

if ( ! preg_match(‚Äú/^[a-z0-9:_\/-]+$|/i‚ÄĚ, $str))

I add | (pipe) character on the example above

Note: In most cases you can resolve the problem just by clearing the cookies