Seach

Showing posts with label Php. Show all posts
Showing posts with label Php. Show all posts

how to create a http post request using curl in php

<?php
$url ='http://localhost/curl/data.php'; //data to be dumped
$data = array
(
'name'=>"xxx",
'age'=>'xxx',
'gender'=>'xxxxx',
'phone'=>'xxxxxxxxxxx'
);
$curl = curl_init(); //curl initalizing
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL =>$url,
    CURLOPT_POST => 1, //setting it to post request
    CURLOPT_POSTFIELDS =>$data
));//option array
$resp = curl_exec($curl); //curl executed
$error = curl_error($curl); //getting error
if(!$error)
{
print_r($resp);
}
curl_close($curl); //closing the curl
?>

post curl file

how to display images and genderate popup image when click using jqueryui,php,mysql

imagedisplay.php
<link rel="stylesheet" type="text/css" href="css/stylesheet.css">
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
      <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
      <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<?php
@mysql_connect("localhost","root","");
@mysql_select_db("database");
$query="select * from gallery";
$temp=@mysql_query($query);
?>
      <div id="imagedisplay" >
    <img src="#" id="dialogimage">
</div>

<div id="image">
    <?php while ($result=@mysql_fetch_assoc($temp))
    {
        //echo $result["imagepath"];
        ?>
    <a href="imagedisplay.php?id=<?php echo $result["id"];?>" id="atag">
    <img src="<?php echo $result["imagepath"];?>">
    </a>
    <?php }?>
</div>
      <script type="text/javascript">
      $(document).ready(function(e){
           $( "#imagedisplay" ).dialog({
               autoOpen: false,
               width:700,
               height:00,
               model:true,
               resizable:false
            });
            $(".ui-button").click(function(e){
               $("#image").show();
            });
          $("a").click(function(e){
              e.preventDefault();
             
              var id="imagedisplayscript.php?id="+this.href.slice(-1);
            
             $.ajax(
                     {
                         url:id,
                 processData: false,
                 cache: false,
                 type: 'POST',
                 success: function (data, textStatus, jqXHR) {
                   
                     $("#dialogimage").attr("src",data);
                       //$("#imagedisplay").css(" background-image", 'url("'+data+'")');
                       $("#dialogimage").css("height","500px");
                       $("#dialogimage").css("width","500px");
                         $( "#imagedisplay" ).dialog( "open" );
                         $("#image").hide();
                    }
                     });
                     });
      });
      </script>

stylesheet.css
#image
{
    width: 500px;
    height: 500;
    border-bottom-style: inset;
    border-bottom-width: 1px;
    border-bottom-color: black;
}
img
{
    height: 200px;
    width: 200px;
    border: black;
    border-bottom-style: double;
}
.ui-widget-header,ui-state-default,ui-button
{
            background:activeborder;
               // #b9cd6d;
            border: 1px solid black;
            color: #FFFFFF;
            font-weight: bold;
          
}
.ui-dialog ,ui-widget,ui-widget-content,ui-corner-all,ui-front ,ui-draggable,ui-resizable
{
    height: 800px;
    width: 800px;
}
.ui-dialog-content,ui-widget-content
{

    height: 520px;
    width: 495px;
}


imagedisplayscript.php
<?php
@mysql_connect("localhost","root","");
@mysql_select_db("database");
$query="select * from gallery where id=".$_REQUEST["id"];
$temp=@mysql_query($query);
$result=@mysql_fetch_assoc($temp);
echo $result["imagepath"];
?>




















how to create a autocomplete text box using php,jquery,mysql

select.php
<?php
@mysql_connect("localhost","root","");
@mysql_select_db("example");
$query="select * from auto";
$tempdata=@mysql_query($query);
$name=array();
while($data=@mysql_fetch_assoc($tempdata))
{
$name[]=$data["name"];
}
echo json_encode($name);
?>

page.php
<head>
    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
      <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
      <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>


    <div class="ui-widget">
    <input type="text" id="automplete-1">
    </div>

<script type="text/javascript">
$(document).ready(function()
{
    var name=<?php include 'select.php'; ?>;
    $("#automplete-1").autocomplete({
        source: name,
        autoFocus:true
    });
});
</script>


how to upload and display image before submiting using php and jquery

main.php
<head>
    <link href="css/style.css" type="text/css" rel="stylesheet"/>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<form method="post" enctype="multipart/form-data" action="upload.php" id="form1">
    <div id="imagePreview">
       
    </div>
<input id="uploadFile" type="file" name="image" class="img" />
<input type="submit" name="save" id="save">
</form>
<script type="text/javascript">
$(document).ready(function(){
    $("#uploadFile").on("change", function()
    {
        var files = !!this.files ? this.files : [];
         // no file selected, or no FileReader support

        // only image file
            var reader = new FileReader(); // instance of the FileReader
            reader.readAsDataURL(files[0]); // read the local file
          reader.onloadend = function()
            { // set image data as background of div
                $("#imagePreview").css("background-image", "url("+this.result+")");
            }
       
    });
    $("#form1").on("submit",function (e)
    {
        e.preventDefault();
        $.ajax(
                {
                    url:"upload.php",
                    type:"post",
                    cache: false,
                    contentType: false,
                    processData: false,
                    data:new FormData(this),
                    success: function (data, textStatus, jqXHR) {
                        alert(data);
                    }
                });
    }
                );
  
   
});
</script>

upload.php
<?php
if(is_array($_FILES))
{
    $destination="image/".$_FILES["image"]["name"];
    $source=$_FILES["image"]["tmp_name"];
   if( move_uploaded_file($source, $destination))
   {
       echo "image uploaded";
   }
   
}

?>

style.css
#imagePreview
{
width: 180px;
    height: 180px;
    background-position: center center;
    background-size: cover;
    -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, .3);
    display: inline-block;
}

How to create a simple captcha

<?php
session_start();
$code=rand(1000,9999);
$_SESSION["code"]=$code;
$image=imagecreatetruecolor(50,24);
$background_color=imagecolorallocate($image,22,100,165);
$forground_color=imagecolorallocate($image,225,225,225);
imagefill($image,0,0,$background_color);
imagestring($image,5,5,5,$code,$forground_color);
header("Cache-Control:no-cache,must-revalidate");
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>


call this file in <img src="above file.php" />

how to display file content using ajax php

function select_browser()
{
    //alert("executed");

    var xmlhttp;
    if(window.XMLHttpRequest)
    {
        //alert("browser is internet firefox");
        xmlhttp=new XMLHttpRequest();
        return xmlhttp;
    }
    else
    {
       
        xmlhttp=new ActiveXobject("MICROSOFT.XMLHTTP");
        return xmlhttp;
    }

}

function load_file()
{
   
   
    ajax.onreadystatechange = function()
    {
        if(ajax.readyState == 4 )
        {
            document.getElementById('message').innerHTML=ajax.responseText;
        }
    }
    ajax.open("POST","file.txt");
    ajax.send();
   
}



user exist or not using php ajax

create two php files user_existance_check.php,user validate.php

user_existance_check.php
 <script type="text/javascript">
var obj=null;
if(window.XMLHttpRequest)
{
    obj=new XMLHttpRequest();//used to handle safari,apple,firefox,chrom etc
}
else if(window.ActiveXobject)
{
    obj=new ActiveXobject("Microsoft.XMLHTTP");//internet exploror browser
}
else
{
    throw new Error("Browser Not Support Ajax");
}
function user_check()
{
   
    obj.onreadystatechange=function()
    {
        if(obj.readyState==4 && obj.status==200)
        {
            //alert("completed");
            document.getElementById("message").innerHTML=obj.responseText; //result from the user_validate.php
        }   
    }
    var name_=document.getElementById("name").value;
    obj.open("POST","user_validate.php?name="+name_);//data senderd method is post we can use get also
    obj.send(null);
   
}



</script>
<html>
<body>
<form method="post" name="form1">
<label>Userid</label><input type="text" name="name" id="name" onKeyPress="user_check()"/>//
<label id="message"></label>
</form>
</body>
</html>

user_validate.php

<?php
@mysql_connect("localhost","root","");
@mysql_select_db("test");
$query="select sname from register where sname='".$_REQUEST['name']."'";
$temp_result=@mysql_query($query);
$count=@mysql_num_rows($temp_result);
if($count===0)
{
    echo "user name avaliable";
}
else
{
    echo "user name is not available";
}

?>




how to update database in php using oops concept


public function update($array,$table,$id)//1st paramerter is associative array,2nd is table name,3rd is id
{
    $query="UPDATE $table SET ";
    foreach($array as $key=>$result)
    {
        $query.=$key."="."'$result'".","; //here $key is list of column names and $result is the value corresponding then
       
    }
    $str=strlen($query)-1; //used to find out index of the last , in the $str field
$temp_query=substr_replace($query," ",$str);//used to replace , with " "
$temp_query.=" WHERE id=".$id;
$status=$this->execute_query($temp_query); //executing the result see previous post for what is execte_query()
return $status;
   
}

how to count number of data in a table using oops in php

public function count_data($table)
{
   
    $query="SELECT * FROM $table";
    $result=$this->execute_query($query);
    return @mysql_num_rows($result);   
}

how to select database through oops concept

public function selectdb($dbname)
{
    $this->dbname=$dbname;
if($this->status)
{
    @mysql_select_db($this->dbname);
}
}
you must add class structure before using it create $dbname variable @ prefix used before mysql_select_db for faster execution

how to insert data to database using object oriented approach any table data can be inserted using this code block

public function execute_query($query)
{
    return  $result=@mysql_query($query);
   
}
public function insert($table,$data)

    while( @list($key,$value) = @each($data))
    {
        $field_names[] = $key;
            if((strpos($value,'now()') === false ) and (strpos($value,'date()')=== false) and (strpos($value,'DATE_ADD')=== false))
            $field_values[] = "'$value'";
            else
            $field_values[] = $value;
    }
$query = "INSERT INTO $table (";
$query .= implode(', ', $field_names);
$query .= ") VALUES (" . implode(",", $field_values) . ")";
if($this->execute_query($query))
{
    return true;
    }
    else
    {
        return false;
    }

}

how to connect to database in php using object oriented approach

class database
{
private $dbname;
private $host;
private $user;
private $password;
private $status;
private $result;
private $connection;
public function connect($hostname,$username,$password)
{
    $this->host=$hostname;
    $this->user=$username;
    $this->password=$password;
    if($this->connection=@mysql_connect($this->host,$this->user,$this->password))
    {
        $this->status=true;
    }
    else
    {
    echo "cannot connect to server ".$this->host;
    }       
}
}
at the function call just provide 3 parameters according to your database $this is used to access class variables




text area control in php

<html>
<body>
<form name="form1" method="post" >
<textarea rows="5" cols="50" name="textarea"></textarea>
<input type="submit" name="submit_" value="Show">
</form>
</body>
</html>
<?php
if(isset($_REQUEST['submit_']))
{
    echo 'textarea data <br/>'.$_REQUEST['textarea'];
}


?>

radio button example in php

<html>
<body>
<form name="form1" method="post" >
<label>Are You A Student Or Not</label>&nbsp;
<input type="radio" name="student" value="Yes">Yes</input>&nbsp;<input type="radio" name="student" value="No">
No</input>
<input type="submit" name="submit" value="Show">
</form>
</body>
</html>
<?php
if(isset($_REQUEST['submit']))
{
if(isset($_REQUEST['student']));
{
echo $_REQUEST['student'];
}
}


?>

password control in php

<html>
<body>
<form name="form1" method="post">
<label>Enter Your Password&nbsp;</label><input type="password" name="text">
<input type="submit" name="button" value="Login">
</form>
</body>
</html>
<?php
if(isset($_REQUEST['button']))
{
    echo $_REQUEST['text'];
   
}
?>

multiple choice using list control in php

<html>
<body>
<form name="form1" method="post" >
<select name="choice[]" multiple="multiple">
<option>Apple</option>
<option>Banana</option>
<option>Mango</option>
<option>Grapse</option>
</select>
<input type="submit" name="save" value="Save">
</form>
</body>
</html>
<?php
if(isset($_REQUEST['save']))
{
    foreach ($_REQUEST['choice'] as $result)
    {
        echo '<br/>'.$result;
    }
}




?>

list control example in php

<html>
<body>
<form name="form1" method="post">
<select name="choice">
<option>Apple</option>
<option>Banana</option>
<option>Mango</option>
<option>Grapse</option>
</select>
<input type="submit" name="save" value="Save">
</form>
</body>
</html>
<?php
if(isset($_REQUEST['save']))
{
    echo $_REQUEST['choice'];
}

?>

hidden field control example

<html>
<body>
<form name="form1" method="post">
<input type="hidden" value="Hithin Chandran" name="data">
<input type="submit" name="show" value="Show">
</form>
</body>
</html>
<?php
if(isset($_REQUEST['show']))
    echo $_REQUEST["data"];
?>

how add a value to a textbox

<?php
$result="hi how are you";
?>
<html>
<body>
<form method="post" name="form1">
<label>Data</label>
<input type="text" value="<?php echo $result;?>" name="text" readonly="readonly">

</form>
</body>
</html>


checkbox example in php

<html>
<body>
<form method="post" name="form1">
<input type="checkbox" name="checkbox1" value="Reading" >Reading</input>
<input type="checkbox" name="checkbox2" value="Writting">Writting</input>
<input type="checkbox" name="checkbox3" value="Fighting">Fighting</input>
<input type="submit" name="display" value="Show">
</form>
</body>
</html>
<?php
if(isset($_REQUEST['display']))
{
    echo 'hobbies are:<br/>';
    for($i=1;$i<=3;$i++)
    {
        $temp="checkbox".$i;
   
        if( isset( $_REQUEST[$temp]))
        {
        echo "<br/>".$_REQUEST[$temp];
        }
    }
}
?>