设置PHP的会话没有表单提交 [英] set PHP's session without form submit

查看:93
本文介绍了设置PHP的会话没有表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个两个按钮的textarea。一个按钮将预览我输入的文本,另一个按钮将文本提交给服务器。这是我的代码:

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

< textarea rows =5name =ShortDescriptionid =ShortDescription>< / textarea>& nbsp
< input type =buttonname =Previewvalue =预览说明>
< input type =submitname =Submitvalue =Submit>
< / form>

我是PHP和JavaScript的初学者。我知道这个问题可以通过使用AJAX来解决。不过,我对AJAX很陌生。任何人都可以帮助我吗?

解决方案

据我所知,您正在启动一个会话,之后,您要创建一个$ _SESSION变量基于某人在文本区域中键入的内容。假设你不希望页面在用户点击预览时改变(但你希望能够在将来的情况下使用$ _SESSION变量),你需要使用AJAX之类的东西(如你所提到的)。我发现最简单的方法就是将jQuery和AJAX一起使用。

基本上,您将创建一个JavaScript函数(使用jQuery)来选择何时单击预览按钮。它看起来像这样。首先,我们在输入标签内添加id ...

 < input type =buttonid =previewIDname =预览value =预览说明> 

接下来我们在javascript文件中创建函数...

  $('#previewID')。click(function(){
request = $ .ajax({
url:/ form。 php,
类型:post,
data:serializedData
});
});

form.php可以简单地作为检查是否存在$ _POST ['ShortDescription']的形式,如果有的话,将其清理并存储在$ _SESSION ['ShortDescription']中。 serializedData只是正在传递的文本区域的内容。 (它实际上只是通过URL传递变量内容,例如,如果ShortDescription =HelloThere,则序列化数据将为 ShortDescription = HelloThere ,它附加到网址就像......



www.website.com/form.php?Sortortlication=HelloThere



请记住,您通常不会手动设置,只是为了展示这个概念。使用类似于:

  var serializedData = $ form.serialize(); 

查看此问题以获取示例AJAX请求



免责声明:可能有更好的方法来达到这个目的,但我想我会提供一种可能性,因为没有人回答过!


I have a textarea with two buttons. One button will preview the text that I entered, and the other one submits the text to server. Here is my code:

<?php
session_start();
$_SESSION['ShortDescription']='';
if(isset($_POST['Submit']))
{...}
?>

<form name="news" action="add_news.php" method="post">
<textarea  rows="5" name="ShortDescription" id="ShortDescription"></textarea>&nbsp
<input type="button" name="Preview" value="Preview Description">
<input type="submit" name="Submit" value="Submit">
</form>

I am a beginner in PHP and JavaScript. I know the problem can be solve by using AJAX. However I am very new to AJAX. Can anyone help me please?

解决方案

From what I understand, you're initiating a session and afterwards, you want to create a $_SESSION variable based on what someone types in the text area. Assuming you don't want the page to change when the user clicks preview (but you want to be able to use the $_SESSION variable in future situations) you'll need to use something like AJAX (as you mentioned). The easiest way I've found to accomplish something like this is to use jQuery and AJAX together.

Basically, you'll create a JavaScript function (using jQuery) to select whenever the preview button was clicked. It will look something like this. First we add the id within the input tag...

<input type="button" id="previewID" name="Preview" value="Preview Description">

Next we create the function in the javascript file...

 $('#previewID').click(function() {
   request = $.ajax({
    url: "/form.php",
    type: "post",
    data: serializedData
    });
});

form.php could simply be a form that checks to see if $_POST['ShortDescription'] exists, if it does, sanitize it and store it in $_SESSION['ShortDescription']. The "serializedData" is just the contents of the text area that is being passed. (which really is just passing the variable contents over the URL. For example, if ShortDescription = "HelloThere", the serialized data would be "ShortDescription=HelloThere", which appends to the URL like...

"www.website.com/form.php?ShortDescription=HelloThere"

Keep in mind that you would not typically manually set this though, that was just to show the concept. Use something like:

    var serializedData = $form.serialize();

View this question for an example AJAX request.

Disclaimer: There may be a better way to accomplish this, but I figured I would offer one possibility since no one had answered yet!

这篇关于设置PHP的会话没有表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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