Click here to Skip to main content
15,891,751 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi This is my javascript code
JavaScript
function login()
{
var c=document.getElementById("txtFirstName").value;
	var x="<?php $con=mysql_connect("localhost","root","");
		mysql_select_db("my_db");
		$result=mysql_query("call PrcEmployeeInsert('c' ,'leeee')");
		if($result)
		{
			$v="inserted";
		}
		echo($v);	?>";
	alert(x);
	return false;
}


What i need is i have to access the variable c in my php coding. How to do this? Pls help
Posted
Updated 19-Nov-12 23:59pm
v2

1 solution

Hello Red Chocolate,


I am giving you this code but actually JS is client side language & PHP is server side language so its not possible to assign client side variable to server side.

I have using Ajax in code so that Ajax will assign that variable in PHP code.


XML
<?php if(isset($_REQUEST['query'])): ?>
<script type="text/javascript" id="runscript">
    function test4 () {
        var x="<?php
            echo $_REQUEST['query'];
            $con=mysql_connect("localhost","root","root");
            mysql_select_db("evrylife");
            $result=mysql_query("call PrcEmployeeInsert('".$_REQUEST['query']."' ,'leeee')");
            //$result=mysql_query("select * from users where id='".$_REQUEST['query']."'");
            if($result)
            {
                $v="inserted";
            }
            echo($v); ?>";
        document.write(x);
    }
</script>
<?php
die;
endif; ?>

<div id="response"></div>
<input type="hidden" id="txtFirstName" value="test" />
<script type="text/javascript">
var test = "<?php echo (isset($_REQUEST['query'])?$_REQUEST['query']:'test2'); ?>";
function login()
{
    var c=document.getElementById("txtFirstName").value;
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState==4) {
            document.getElementById('response').innerHTML = xmlhttp.responseText;
            eval(document.getElementById('runscript').innerHTML);
            test4();
        }
    }
    xmlhttp.open("GET",'test2.php?query='+c,false);
    xmlhttp.send(null);
}
login();
</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900