0

Ok I am confused with php, javascript and html and dont know what to do. On researching on the internet, i found js is client side and php is server side. when a php file is run on the browser, it converts everything into html and the page is loaded. Now let me tell you guys what i am doing.

I have a php file that give me some stats from a particular url (in the sample i am just showing url)

<?
$url="www.example.com";
echo "URL = " .$url;
?>

Result URL = www.example.com
The above code echoes the url which is www.example.com. I added a textbox to this code which i believe is javascript+html

<script>
function myFunction() {
$url=myurl.value;
}
</script>

<input type="text" name="myurl" id="myurl">
<input onclick="myFunction()" type="submit" name="btnurl" id="btnurl" value="Submit">
<br><br>

<?
$url="www.example.com";
echo "URL = " .$url;
?>

Here the result is same. only difference is that it has a textbox and button above the result.
When I enter another url in the textbox and press submit, it does nothing probably because the page is already loaded. I want to replace the result of www.example.com to the one which is entered in the textbox without changing the .php file. There will always be a default url in the .php file. whenever the file is opened in the browser, the default statistics will be shown... only when the user enters new url and clicks submit, the stats should change.

How can I achieve this? I am behind this since more than a couple of hours now and not sure how to get this done. Please help me.... Thank you.

EDIT Can I have two .php files? one for the user to enter url and submit and another one to get the entered url and echo it? If yes, how? If I understand this logic, i can get a start for what I am doing.

3
  • You are mixing PHP and JavaScript. If you are looking to send data to a PHP file, look up XMLHttpRequest. Commented Oct 31, 2014 at 19:06
  • That is what i thought. i am not sending data to any file. I dont know how to explain. when the page is loaded, it will show statistics of a url and on the same page if i change the url, the stats should change on the same page without getting redirected. stats here would mean only echoing contents of $url variable. Commented Oct 31, 2014 at 19:09
  • Yes you can have 2 php files. Commented Oct 31, 2014 at 20:46

2 Answers 2

3

I think you are trying to do more with your js function, but syntactically it is combining js and php. It should look like this

function myFunction() {
   var url = document.getElementById('myurl').value;
}

Although this doesn't really do anything other then assign the content of the text box to a variable.

EDIT

<script>
    function myFunction() {
       document.getElementById('url').innerHTML = document.getElementById('myurl').value;
    }
</script>

<input type="text" name="myurl" id="myurl">
<input onclick="myFunction()" type="submit" name="btnurl" id="btnurl" value="Submit">
<br><br>
<? $url = "www.example.com"; ?>
URL = <span id="url"><?= $url; ?></span>
Sign up to request clarification or add additional context in comments.

9 Comments

i tried this but as you said it doesnt do anything. I want the initial echo that is displayed on initial loading of the page to be replaced to the one entered in the textbox.
Then you need to put that url into a span (or some kind of html element) with an id and change that. php would be this <span id='url'><?php echo $url; ?></span>. js would be document.getElementById('url').innerHTML = url. This would be line two of your myFunction() function
I updated my answer to include my comments above, although this still doesn't really do anything other than change the text on the page
It is working as per my desire. I am trying to understand the logic. Thanks
definitely.. however after understanding the logic, i had a question... is it just replacing the content of url or actually putting new textbox value in the $url variable? The page i have shown is just a sample, the actual php outputs a table with lot of statistics that is processed from the $url variable. when the variable get new value from the textbox, will the page reload so that my table with lot of statistics can be processed again with new url?
|
1

natzim is correct if you are wanting to write the url back to the php file. If you use javascript to change the action of the form, it will submit to a different page.

//javascript
function myFunction() {
   //this should change the page that loads after submit.
   //If you want to go to a new page that the user enters, leave this code in...
   //If not, remove it
   document.getElementsByTagName("form")[0].action = document.getElementById("myUrl").value;
}

That is assuming you have a form tag somewhere (which you will need to submit the page). Also I am not sure this code will run if you use a submit and not a button. If you used a button instead you could append this to the code above to submit the form:

//This would be part of your myFunction if you used a button instead of a submit input
document.getElementsByTagName("form")[0].submit();

as per my comment -

this code is your old php:

<?
$url="www.example.com";
echo "URL = " .$url;
?>

and this is the php I suggested:

<?php
$url=isset($_POST['myurl']) ? $_POST['myurl'] : 'www.example.com'; 
echo "URL = " .$url; 
?>

this would check the myurl input from that was submitted to the server and set the value of $url to its value if it existed then the $url variable would be echoed to the page under the inputs.

This code is assuming you are using the POST method rather than the GET method when your form was submitted.

**EDIT: **

To clarify - here is your page with the modifications I am suggesting. (Please ignore the javascript above as it seems you will not need it):

<form action='www.example.com' method='post'>

<input type="text" name="myurl" id="myurl">
<input type="submit" name="btnurl" id="btnurl" value="Submit">
<br><br>

<?php
$url=isset($_POST['myurl']) ? $_POST['myurl'] : 'www.example.com'; 
echo "URL = " .$url;
?>

</form>

9 Comments

I tried this, it clears off the initial echoed url and on the address bar I have example.com/test.php?myurl=ddddd&btnurl=Submit where as i need newly entered url to be echoed under the textbox and the address bar should only have example.com/test.php
I understand what you are saying however still confused a bit. Is it possible for your to update your answer with two codes 1.php and 2.php. This will give me a better understanding. Thanks I am confused with the "did not want to navigate away from that page" comment
ok i am going to try this. but i still did not understand what you meant when you said "if you did not want to navigate away from that page you can simply remove the line that sets the form action and it will reload the same page" i do not see this in your revised code. Thanks
I'm sorry I must have missunderstood the original extent of the question. That was just something extra that you will not need for this case. That would send the user to a new page rather than just reloading the current page. Since you just want to reload the current page, you can ignore that piece of code.
I understand that. actually i wanted to do it all in one php file without redirects since i was not getting a desirable answer i asked for an alternative solution with two php files that you gave. i edited the question asking for an alternative solution because i am not getting a proper solution to reload the same page by just changing the variable based on the new url entered in the textbox.
|

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.