Click here to Skip to main content
14,639,515 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi This is my javascript code
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.


<?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>
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100