会话在原始但不在循环副本上正常工作

时间:2017-10-18 02:46:21

标签: php jquery ajax

这适用于循环,但是循环第一个可以工作,但第二个不能像第一个那样设置PHP会话值,所以如何让循环副本像第一个一样工作意思是能够设置PHP会话值。

*注意:这是不是HTML表格这是通用代码我和我的朋友来自一个表格来设定会话价值的教程。*

教程来源:https://www.formget.com/submit-form-using-ajax-php-and-jquery/#

我不想使用表格,不要问我为什么是个人的,所以如何解决这个问题,循环复制也可以设置会话变量的会话值i&# 39;我这样说是因为只有第一个能够做到这一点而第二个不能像第一个那样设置会话值。

.gif Screenshot

submit_session.php

<?php 
session_start(); 
?>

<!DOCTYPE html>
<html>
<head>
<title>Simulated Form</title> 
<meta name="viewport" content="width=device-width">
<meta charset="UTF-8">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>

//This section converts the HTML id's in the form div into form values

$(document).ready(function(){  
$("#execute").click(function(){
var execute= $("#execute").val();
var name= $("#name").val();
var age= $("#age").val();

var dataString = 'execute='+ execute + '&name='+ name + '&age='+ age;


//AJAX code to submit the simulated form.

$.ajax({
type: "POST",
url: "submit_session.php",
data: dataString
});
});
});
</script>

<style>

.form {
background-color: gold;
width: 200px;
margin: auto;
border-radius: 5px;
padding-top: 5px;
}

.set-value{
background: blue;
padding: 1px 4px;
text-decoration: none;
border: 1px solid gray;
color: white;
margin: auto;
display: block;
width: 100px;
text-align: center;
border-radius: 5px;
}

  .name-input {
    margin: auto;
    display: block;
  }

  .age-input {
    margin: auto;
    display: block;
  }


</style>

</head>

<body>
<?php

//Generate more than one copy of the simulated form
for ($loop = 0; $loop <= 1; $loop++) {

?>

<br> 

<!--The simulated form-->

<div class="form">
<p style='text-align: center;'>Simulated form</p> 
<input type="text" id="name" class='name-input' required="required" placeholder='Name' autocomplete="off">
<br>
<input type="text" id="age" class='age-input' required="required" placeholder='Age' autocomplete="off">
<br>
<!-- Simulated form submit button -->  
<a  class="set-value" id="execute" href="view-session-values.php">Set value</a>
<br>
</div>

<?php

echo $loop;
}

//Set session values from a simulated form

if(isset($_POST['execute'])){
$name = $_POST['name'];
$age = $_POST['age'];
if($check=1){
$_SESSION['name']=$name;
$_SESSION['age']=$age;
echo "<script>window.open('_self')</script>";
}
}
?>

</body>
</html>

视图会话values.php

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

<!DOCTYPE html>
<html>
<head>
<style>

  #set-new-session-value {
    background-color: black;
    color: white;
    padding: 5px 5px;
    text-decoration: none;
    border-radius: 5px;
  }

  #destroy-session-value{
    background-color: black;
    color: white;
    padding: 5px 5px;
    text-decoration: none;
    border-radius: 5px;
  }

</style>
</head>

<body>

<!--This section is based on choosing session options -->
<br>
<a id='set-new-session-value' href="submit_session">Set new session value</a>
<a id='destroy-session-value' href="destroy_session.php">Destroy session value</a>
<br>

<!--This section is where the set session values are shown-->
<p><?php echo $name; ?></p
<p><?php echo $age; ?></p>  

</body>

</html>

0 个答案:

没有答案