Kut
Kut

Reputation: 211

PHP Javascript variable help

Is there any way I could get the value of a html text field without using GET or POST or REQUEST? Alternatively, is there any way to get the field value in the same form or page else where.

This works with direct value such as "james", "system" and so on. the only problem is how do i make it work with html field values

Like:

<input type = "submit" onclick = "
<?php $username = "kut"; 
$result = checkname($username); 
if($result) 
{
?> alert("success"); <?php 
} 
else {?> alert("failed"); <?php 
}?> 
">

How can i replace "kut" with the value of a text field with id = "username" ?

<?php $username = "?>document.getElementById('username').value;<?php"?>

or something like that...???

In short, I need to get the value of a html field else where in the same page inside a javascript function, using PHP... like in the above javascriptFunction(), function

Upvotes: 0

Views: 380

Answers (3)

Kornel
Kornel

Reputation: 100170

You have fundamental misunderstanding of how client-server architecture works.

PHP can be executed thousands of miles away, even days apart, from place where and when JavaScript does.

First PHP generates whole page, all of HTML, all of JavaScript source code (unexecuted), and then, after PHP is done and gone, browser starts running JavaScript.

These two can't be mixed together like you wanted, even though it may seem so in the PHP source code.

Although you can communicate with the server again using AJAX or similar, you probably should first understand how client-server architecture works and try to solve the problem without AJAX (e.g. handle all of it on server side, or all on client side).

Upvotes: 2

subhaze
subhaze

Reputation: 8855

You can not directly call a PHP function in JavaScript. You could set a JavaScript value from php before the page loads via echo. PHP is executed on the server while JavaScript is executed on the client side.

Upvotes: 1

JAL
JAL

Reputation: 21563

1> I suggest using jQuery to handle the Ajax part.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script>
    function check_user(){
      var user_el=document.getElementById('username');
      if(!user_el){ return false; }
      var username=user_el.value; // this could all be replaced with $('username').val()
      $.getJSON('check_var.php',{"user":username},function(data){
            if(data.result=='error'){ alert('something was wrong with the PHP stuff'); }
            alert(data.userstatus);
            });
      }
 </script>

2> On the PHP side, as check_var.php, you need a script that takes the username input, checks the DB, and sends back the result as JSON data.

<?php
if(!isset($_GET['user']){ exit; }

$username=preg_replace('#['^\w\d]#','',$_POST['user']);

//do your database query. I assume you have that part all set.
//since I'm not filling in all of that, you'll need to fix this next part to work with your system
//let's pretend it's like $found=check_user($username);
//be sure to use mysql_real_escape_string or prepared statements on the $username var since you're working with user input

$status=$some_db_error ? 'error' : 'success';

$results=array('result'=>$status,'userstatus'=>$found);
header('Content-Type: application/json');  
echo json_encode($results);

Upvotes: 0

Related Questions