通过会话连接HTML、Javascript和PHP

通过会话连接HTML、Javascript和PHP,javascript,php,html,session,Javascript,Php,Html,Session,我正在尝试使用PHP、HTML和Javascript为作业创建一个购物车,但我甚至很难让PHP的基础知识正常工作 因为这是一个作业,我不会发布我的确切代码,但我会发布一些修改过的代码,描述我正在努力解决的一般概念。还要注意的是,我在这篇文章中大大简化了程序,但我认为我已经包括了所有相关的内容 首先,我有一个HTML表单,带有选择选项标签: <?php session_start(); ?> <!-- This is on the first line of the page -

我正在尝试使用PHP、HTML和Javascript为作业创建一个购物车,但我甚至很难让PHP的基础知识正常工作

因为这是一个作业,我不会发布我的确切代码,但我会发布一些修改过的代码,描述我正在努力解决的一般概念。还要注意的是,我在这篇文章中大大简化了程序,但我认为我已经包括了所有相关的内容

首先,我有一个HTML表单,带有选择选项标签:

<?php session_start(); ?> <!-- This is on the first line of the page -->
...
<form id="formexample" method="post" action="cart.php">
<select name="formexample2">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>
<select  name="formexample2">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>
<button type="button" onclick="addToCart()">Add to Cart</button>
</form>
<div class="test"></div>
然后我将其写入Javascript函数:

var test = "<?php echo $PHPtest; ?>";
alert(test);
我还移动了主页顶部的“include”cart.php。查看Josh回复下的讨论,了解我是如何来到这里的

警报显示“未定义”

编辑2 我已根据以下答案更新了我的函数:

这就是我现在拥有的:

function addToCart()
{
    var test;
    $.ajax(
    {
        url:"cart.php",
        type:"post",
        data: data,
        success:function(PHPtest) 
        {
            test = $.parseJSON(PHPtest);
            console.log(test); //sends the response to your console
        },
        error:function() 
        {
            console.log("Error");
        }
    });
    alert(test);
}

我尝试了“PHPtest”和“test”的各种组合,但都没能成功。现在我没有收到任何警报。

使用对PHP文件的Ajax调用来处理实时页面和PHP

PHP在进入浏览器之前被处理。你不能在飞行中使用它

如果没有一点技巧(例如AJAX调用),就不能混合使用前端和后端脚本

编辑

如果使用jQuery,那么您的脚本正在寻找帖子

$.ajax({
    url:"script/location/run.php",
    type:"post",
    data:"name=value&your=post&data=goeshere",
    success:function(response) {
        console.log(response); //sends the response to your console
    },
    error:function() {
        console.log("um, error with the script, or it can't be found");
    }
});
编辑2

购物车的设置应与此类似

<?php
session_start(); //On any PHP document (that are not includes or require) this is required at the top of the page to use sessions

$PHPtest= 1;


$_SESSION['cart']['formexample1'] = $_POST['formexample1'];
    $_SESSION['cart']['formexample2'] = $_POST['formexample2'] ;

/* Note that in my actual code the form has 11 different select tags, some with up to ten options. I'm storing the information as a big multidimensional array in $_SESSION for ease. */

?>
你的JS

var cart = '';
$.ajax({
    url:"script/location/run.php",
    type:"post",
    data:"name=value&your=post&data=goeshere",
    success:function(response) {
        //response will hold the JSON value of your cart
        cart = $.parseJSON(response); //decodes to a JSON object in JavaScript;

        //you can access the cart variables like this
        console.log(cart.formexample1);

    },
    error:function() {
        console.log("um, error with the script, or it can't be found");
    }

当您执行
警报(“”
)时会发生什么情况?警报显示引号之间的确切内容,即它显示角括号、问号和其他内容。您是否在html页面上的
标记中使用它?Javascript包含在“”中。然后,cart.php包含在一个Javascript函数中,并带有“$(“.test”).append(“”;”。也许您应该像这里一样使用
load()
函数-so…
$(“.test”).load(“path/to/cart.php”)。可能是因为php没有正确加载,所以无法正常工作。谢谢。我现在正在读关于AJAX的书。如果还有任何问题,我会回来。我已经将cart.php的include移到了文件的开头,并在cart.php的开头添加了“if(isset($\u POST)){…}”,以避免出现错误。然后我想我需要使用AJAX函数来检索会话变量的值,但我不确定要使用哪一个-从我所阅读的内容来看,我认为我需要使用“$.get()”?我正在读关于它的()并且对参数感到困惑。“url”显然是.php文件的名称(它在同一个目录中),但我不知道应该在其他参数中输入什么。我不清楚这些示例。另外,请确保ajax调用的脚本也在该页面上有session_start(),否则它将无法与会话交互。因此,我会在url后面加上“cart.php”,在“data”后面加上“$\u session['cart'][$\u POST['formexample1'][$\u POST['formexample2']”?对吗?我会将它存储在另一个变量中,比如“$variable=$.ajax({…});”?谢谢,哇,哇。。回到原点,哈哈。您似乎不理解这些概念$_会话['cart'][$\u POST['formexample1']=$\u POST['formexample1'];应该是$\u SESSION['cart']['formexample1']=$\u POST['formexample1'];您可以像多维数组一样分配$\会话。
function addToCart()
{
var test;
    $.ajax(
    {
        url:"cart.php",
        type:"post",
        data:"$PHPtest",
        success:function(response) 
        {
            test = $.parseJSON(response);
            console.log(response); //sends the response to your console
        },
        error:function() 
        {
            console.log("Error");
        }
    });
    alert(test);
}
function addToCart()
{
    var test;
    $.ajax(
    {
        url:"cart.php",
        type:"post",
        data: data,
        success:function(PHPtest) 
        {
            test = $.parseJSON(PHPtest);
            console.log(test); //sends the response to your console
        },
        error:function() 
        {
            console.log("Error");
        }
    });
    alert(test);
}
$.ajax({
    url:"script/location/run.php",
    type:"post",
    data:"name=value&your=post&data=goeshere",
    success:function(response) {
        console.log(response); //sends the response to your console
    },
    error:function() {
        console.log("um, error with the script, or it can't be found");
    }
});
<?php
session_start(); //On any PHP document (that are not includes or require) this is required at the top of the page to use sessions

$PHPtest= 1;


$_SESSION['cart']['formexample1'] = $_POST['formexample1'];
    $_SESSION['cart']['formexample2'] = $_POST['formexample2'] ;

/* Note that in my actual code the form has 11 different select tags, some with up to ten options. I'm storing the information as a big multidimensional array in $_SESSION for ease. */

?>
//at the end of cart.php
echo json_encode($_SESSION['cart']); //will convert your session cart to JSON and send to the response variable in your AJAX.
var cart = '';
$.ajax({
    url:"script/location/run.php",
    type:"post",
    data:"name=value&your=post&data=goeshere",
    success:function(response) {
        //response will hold the JSON value of your cart
        cart = $.parseJSON(response); //decodes to a JSON object in JavaScript;

        //you can access the cart variables like this
        console.log(cart.formexample1);

    },
    error:function() {
        console.log("um, error with the script, or it can't be found");
    }