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

share|improve this question

31% accept rate
2  
Your server isn;t running anything unless there's a GET or POST (or whatever verb) happening...what are you trying to do here? – Nick Craver Nov 28 '10 at 16:26
1  
are you attempting to check the text field value before submitting? sounds like you are attempting to do an ajax call. – kjy112 Nov 28 '10 at 16:34
Okay... so... you want to use JavaScript to contact your database via PHP, check whether a value exists, and send the result back to JavaScript. Right? – JAL Nov 28 '10 at 16:45
1  
that's exactly an ajax call would do for you. it basically does a post/get to your php script without refreshing the page. this is done through javascript. take a look at this first. ajaxtutorial.net/index.php/category/ajax-basics – kjy112 Nov 28 '10 at 16:46
I've put in an answer using Ajax to check. It's not clear though, whether this needs to happen with Ajax or whether it can just happen on page load. Hopefully this answers your question, though. – JAL Nov 28 '10 at 17:03
feedback

3 Answers

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).

share|improve this answer
feedback

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.

share|improve this answer
feedback

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);
share|improve this answer
@ ALEX JL: Have the entire db code ready, but need to perform the task in my edited question. – Kut Nov 28 '10 at 17:05
@ ALEX JL: the task in my edited question works with direct value such as "james", "system" and so on. the only problem is how do i make it work with html <input> field values – Kut Nov 28 '10 at 17:07
I'm not really sure what you mean. If you want to deal with a value that you don't have ready when the page loads, like something that has been entered by the user, and you don't want to reload the page, you have to use Ajax. – JAL Nov 28 '10 at 17:16
I'll clearly tell u... as in the above example: if i pass checkname("james"), it queries the db and returns a result, like true and false, and displays an alert box with the result. How do i use a text field value inside checkname,... like checkname(document.getElement.....), where checkname is a php function... – Kut Nov 28 '10 at 17:20
@Kut You can't mix PHP and JavaScript in the way you seem to think is possible. PHP runs on your server. JavaScript runs on the user's browser. So, you can't directly insert a result of a JavaScript function (getElementById) into a PHP function. – JAL Nov 28 '10 at 19:10
show 1 more comment
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.