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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3