0

Hi all i know this question has been posted but being a total noob i couldnt get what answers meant. Please help. I want to pass inputbox value dynamically to a php variable . i am using javascript here please suggest if there's another way without using form submission , _GET or _POST. i want it done dynamically without any submission.

function showHint(str)
    {
    document.getElementById('TK').innerHTML = str;
    var str = str

    }

</script>


<html>
<head>Inputbox</head>
<title>TEST PAGE </TITLE>
<body>
<input type='text' id='TK' name='TK' onkeyup='showHint(this.value)'/>
<?php
$str = var  str ;  
echo "<a href = 'newpage.php?S=$str'/>" ; ?>
</body>
</html>
3
  • 1
    "Hi all i know this question has been posted..." Not the best way to start your question Commented Aug 14, 2012 at 1:00
  • i wish i had expertise in computers, i wouldnt have started the question that way or even asked it but sigh Commented Aug 14, 2012 at 1:02
  • 1
    Google "JQuery .post() method" ... That is exactly what you need. Commented Aug 14, 2012 at 1:04

3 Answers 3

1

No. You can't. PHP is NOT a dynamic language, and it does NOT run client side. PHP runs once, and only once, and that's when the page is loaded. It runs its script and it stops. What you can do is get javascript to do an AJAX call. AJAX is basically a way of passing information to another page and getting the data, all in JavaScript. Do some research on it, but in short, you can't make PHP run once it's already been run

Sign up to request clarification or add additional context in comments.

2 Comments

a working example would be greatly appreciated as my knowledge is very limited. THanks
Well, this examples uses the jquery library, since it makes it easier $.post('phpFileURL', { '_postIndex' : $("#inputElementID").val() }, function(data) { //data is the HTML/Text/Whatever that was echoed by your PHP file });
0
<script type="text/javascript" >
function process(){
var field1 = 'whatever';
var field2 = 'more whatever';

$.post("go.php",{field:field1,bext_field:field2},function(result){
         alert(result); 


});

};
</script>

This will alert out whatever you ECHO from GO.PHP.

You will also need a handler like:

onClick="process();"

on a div, button, image, just about anything you want to "initiate" your post

2 Comments

<script type="text/javascript" > function process(str){ var field1 = str; $.post("ani_block_dev.php?S=V&TK=",{TK:field1},function(result){ alert(result); }); }; </script> <html> <head>Inputbox</head> <title>TEST PAGE </TITLE> <body> <input type='text' id='TK' name='TK' onkeyup='process(this.value)'/> </body> </html> IS this the proper implementation ??
Don't forget you need: <script src="http://code.jquery.com/jquery-1.7.min.js"></script> at the top of your page to include JQuery
0

I would imagine the other answers you found probably would have said the following:

PHP executes before the user has a chance to see the page.

JS let you control what happens after.

Therefore, your problem is that you are trying to use PHP to do something it simply cannot.

Use those points to help guide your decisions when developing your applications. In this case, if you're trying to build a link based on what a user types in a box, your solution to the problem isn't PHP at all (the page is already loaded, you're too late!) -- your solution is JS.

Think about it like this:

/* 
  assumes you already have an <a> on the page.  if not, you'll 
  have to create a new <a> element dynamically.  (google "mdn createElement"
  for help)
*/
function showHint (str) {
    document.getElementById('TK').innerHTML = str;
    var link = document.getElementById('your-a-link');
    link.setAttribute('href', 'newpage.php?S=' + str);
}

7 Comments

var link = document.getElementById('your-a-link'); link.setAttribute('href', 'newpage.php?S=' + str); can you please explain this and what do i need in my html part for this to work
Into your HTML markup, if you add in <a href="#" id="your-a-link">Link Text</a>, var link will "grab" that element ( the same way you used document.getElementById('TK') to "grab" the input. The "setAttribute" reads just like what it does --- it takes the <a> and sets the href attribute to the "str" that came in through your function.
thanks a lot that really helped. i have another question concerning this. is it possible to append a php variable at end of hyperlink on the page ??? like Link Text will be a hyperlink with newpage.php?S=12314 but add V=WEST at the end where west is based on php variable. <a href="#" id="your-a-link"+ &V=$variable>L.ink Text</a>
Again, think of the flow of how your page loads: PHP executes, then interface loads in the user's browser, now JS can function. Try to just make "one hop" as much as possible (PHP -> browser / browser <-> JS). Des $variable change for each link, or will it remain the same? If it needs to change per link, you're looking at a new problem (google ajax). If it can stay the same, probably the easiest thing to do is just add on a print "<script>window.vAttribute = \"".$var."\";</script>"; to the page. Then you can append that to the URL in the same link.setAttribute function we called.
actually it changes based on row of data the hyperlink is in.so i have a table of data with hyperlink at end of each row which fetches all the variables and send them to a new page where sql update statement is run. but i want to include one more variable which i am putting in an inputbox, thats why i need to figure out a way to grab this inputbox variable n append to those hyperlinks. its getting complicated
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.