我试图使用文本框的值作为会话变量 [英] I'm trying to use a textbox value as a session variable

查看:93
本文介绍了我试图使用文本框的值作为会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在按钮被点击时使用文本框的值作为会话变量,到目前为止我已经使用了它,但是它被硬编码了,我怎样才能使用文本框的值?

b
$ b

以下是会话代码:

 <?php 
session_start() ;
$ _SESSION ['url'] =url;
?>

以下是文本框:

  echo< input type ='text'id ='starurl'value =''/>; 
echo< br>< button onclick ='save_a9({$ row99 ['starID']})'>批准< / button><按钮onclick ='save_d9({$ row99 ['starID ']})' >不赞成< /按钮><峰; br>中;

以下是save_a9:

  function save_a9(id){
$ .post('response6.php',{starID:id},
function(result){
alert(result) ;
window.location.reload();
});
}



这是您的意思?该值不需要表单,只需要转到另一个页面即可使用

 <?php 
session_start();
if(isset($ _ POST ['url']){
$ _SESSION ['url'] = $ _GET ['url'];
}
?>

echo< input type ='text'id ='starurl'value ='。htmlspecialchars($ _ SESSION ['url'])


解决方案

当您分配_SESSION url时,您需要使用发布的变量来分配字符串。 p>

  $ _ SESSION ['url'] = $ _GET ['url']; 

要做相反的事情,并让文本框显示会话的值,您可以:

  echo< input type ='text'id ='starurl'value ='。htmlspecialchars($ _ SESSION ['url'])。'/>; 

需要注意的是,如果您希望文本框实际上是 do , ,您需要将输入框包裹在表单标记中。



< form> 标记告诉表单开始和结束的浏览器。您可以在< form> < / form> 标签之间添加各种HTML标签。 (thanks echoecho!)



如果您正在以REST方式工作,那么GET应该用于只获取数据的请求,并且POST应该用于请求发生的请求。



例如:
$ b $ ul

  • 获取显示特定SO问题的页面

  • 发表评论
  • >
  • 点击添加到购物车按钮并发送POST请求。



  • 感谢Skilldrick!

    表单& PHP文件应该如下所示:

     <?php 
    session_start();
    if(isset($ _ POST ['url']){
    $ _SESSION ['url'] = $ _POST ['url'];
    }

    echo'< form action =POSTmethod =?>';
    echo< input type ='text'id ='starurl'value ='。htmlspecialchars($ _ SESSION ['url '])。'/>;
    echo'< / form>';

    当表单更新时,会发生更新,当您关闭浏览器并再次打开时,您会在输入框中看到旧内容(按enter保存输入框,而不显示提交按钮)。


    I'm trying to use a textbox value as a session variable when the button is clicked, so far I have it working but it's hardcoded in, how do I get it to use the textbox value instead?

    Here's the session code:

    <?php
    session_start();
    $_SESSION['url'] = "url";
    ?>
    

    Here's the textbox:

    echo "<input type='text' id='starurl' value=''/>";
    echo "<br><button onclick='save_a9({$row99['starID']})'>Approve</button><button onclick='save_d9({$row99['starID']})'>Disapprove</button><br>";
    

    Here's the save_a9:

     function save_a9(id) {
            $.post('response6.php', {starID:id}, 
            function(result) { 
                alert(result); 
                window.location.reload();
        });
    }
    



    Is this what you mean? the value doesn't need a form, it just needs to go to another page to be used there

    <?php
    session_start();
    if (isset($_POST['url']) {
      $_SESSION['url'] = $_GET['url'];
    }
    ?>
    
    echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url'])"
    

    解决方案

    When you are assigning the _SESSION url, you will need to use the posted variable to assign the string.

    $_SESSION['url'] = $_GET['url'];
    

    To do the opposite, and have the textbox show the value of the session, you would:

    echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url']) . "'/>";
    

    It is important to note that if you want the text box to actually do something, you will need to have the input box wrapped around a form tag.

    The <form> tag tells the browser where the form starts and ends. You can add all kinds of HTML tags between the <form> and </form> tags. (thanks echoecho!)

    If you are working RESTfully, GET should be used for requests where you are only getting data, and POST should be used for requests where you are making something happen.

    Some examples:

    • GET the page showing a particular SO question
    • POST a comment
    • Click the "Add to cart" button and send a POST request.

    (Thanks Skilldrick!)

    The form & PHP file would look like this:

    <?php
    session_start();
    if (isset($_POST['url']) {
      $_SESSION['url'] = $_POST['url'];
    }
    
    echo '<form action="POST" method="?">';
    echo "<input type='text' id='starurl' value='" . htmlspecialchars($_SESSION['url']) . "'/>";
    echo '</form>';
    

    You will notice when the form is updated, the session updates too. When you close your browser and open it again, you will see you old contents on the input box. (Press "enter" to save the input box without a submit button).

    这篇关于我试图使用文本框的值作为会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆