douyong4842 2014-10-31 19:04
浏览 76
已采纳

用文本框输入替换php变量

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.

  • 写回答

2条回答 默认 最新

  • dongzhi9457 2014-10-31 19:16
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值