如何使用php将上一页可点击的url保存在数据库中?

Posted

技术标签:

【中文标题】如何使用php将上一页可点击的url保存在数据库中?【英文标题】:how to save previous page clikable url in database using php? 【发布时间】:2017-03-24 06:55:52 【问题描述】:

我在项目中有以下 div。点击“request for demo”按钮后,它将重定向到下一个窗口到注册表(链接在javascript函数中提到)。单击按钮并保存注册后,我想将相关的 div id 保存在数据库中。

以下是我的 html 和 javascript

<div class="row">
            <div class="col-md-4 col-sm-6 pos-vertical">
                <span><b>Supermarkets, Fruits & Vegetables</b></span>
                <hr>                    
                <p>Uninterrupted fast billing, inventory control, CRM & Loyalty programs makes your shoppers happier</p>
                <button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
                <!--<button type="button" onclick="openWin()" class="btn btn-default" id="demo-btn" data-toggle="modal" data-target="#demoModal">Request for Demo</button>-->
                <button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Supermarkets, Fruits and Vegetables" >Request for Demo</button>
            </div>

            <div class="col-md-4 col-sm-6 pos-vertical">
                <span><b>Grocery & Departmental Stores</b></span>
                <hr>
                <p>POS for grocery, departmental store, hypermarket and convenience store with GSmartPOS</p>
                <button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
                <button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Grocery and Departmental Stores" >Request for Demo</button>
            </div>

            <div class="col-md-4 col-sm-6 pos-vertical">
                <span><b>Pharmacy, Medical Shop POS</b></span>
                <hr>
                <p>Prefilled drug index, update stock position and bill from Day One. Manage batches and expiry with ease</p>
                <button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
                <button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Pharmacy Medical Shop POS" >Request for Demo</button>
            </div>
        <br>
            <div class="col-md-4 col-sm-6 pos-vertical">
                <span><b>Apparel & Footwear</b></span>
                <hr>
                <p>POS Features like matrix-inventory, non-moving stock analysis, can help keep inventory latest and fashionable</p>
                <button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
                <button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Apparel and Footwear" >Request for Demo</button>
            </div>

            <div class="col-md-4 col-sm-6 pos-vertical">
                <span><b>Electrical & Electronics POS</b></span>
                <hr>
                <p>Perfect solution to manage serialized inventory for mobile, computer electrical and electronics shops</p>
                <button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
                <button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Electrical and Electronics POS" >Request for Demo</button>
            </div>

            <div class="col-md-4 col-sm-6 pos-vertical">
                <span><b>Fashion Jewellery Shop</b></span>
                <hr>
                <p>An integrated, modular & scalable POS for furniture, glass & crockeries, opticals, music, toys & baby shop retail</p>
                <button type="button" class="btn btn-default" id="dwmld-btn">Free Download</button>
                <button type="button" onclick="openWin()" class="modalBtn btn btn-default" id="Fashion Jewellery Shop" >Request for Demo</button>

            </div>
        </div>

在每个 div 按钮上调用以下函数,

<script>
function openWin() 
window.open("register.php");

</script>

点击每个 div 按钮后,表单将在新窗口中打开,

<div class="register">

        <div class="container">
        <div class="row main">
            <div class="main-register main-center">
                <form class="" method="post" id="contact-form">                     
                    <div class="form-group">
                        <div class="cols-sm-10">
                            <div class="input-group">
                                <span class="input-group-addon" id="register-icon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
                                <input type="text" class="form-control"  id="name"  placeholder="Enter your Name"/>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">                            
                        <div class="cols-sm-10">
                            <div class="input-group">
                                <span class="input-group-addon" id="register-icon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
                                <input type="text" class="form-control" id="email"  placeholder="Enter your Email"/>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">                            
                        <div class="cols-sm-10">
                            <div class="input-group">
                                <span class="input-group-addon" id="register-icon"><i class="fa fa-phone fa" aria-hidden="true"></i></span>
                                <input type="text" class="form-control" id="contact_no"  placeholder="Enter your contact number"/>
                            </div>
                        </div>
                    </div>
<input type="hidden" id="requestType">
                    <div class="form-group ">
                        <button class="btn btn-primary btn-lg btn-block register-button submit">Register</a>
                    </div>
                </form>
                <span class="success" style="display:none">Thank You for Register with us.</span>
            </div>
        </div>
       </div>
     </div>

表单将使用 ajax 和 php 保存

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>

<script type="text/javascript" >
$(function() 
$(".modalBtn").click(function()
var formName = $(this).attr("id");
$('#requestType').val(formName);
$('.success').hide();
$('.error').hide();
$('#contact-form').show();
$("#demoModal").modal();
);
$(".submit").click(function() 
var name = $("#name").val();
var requestType = $('#requestType').val();
var contact_no = $("#contact_no").val();
var email = $("#email").val();
//alert(requestType);
var dataString = 'name='+ name + '&contact_no=' + contact_no + '&email=' + email+ '&requestType=' + requestType;

    if(name=='' || contact_no=='' || email=='')
     
    $('.success').fadeOut(200).hide();
    $('.error').show();
    
    else
    
   $.ajax(
  type: "POST",
 url: "register.php",
 data: dataString,
 success: function()
 $('.success').fadeIn(200).show();
 $('.error').fadeOut(200).hide();
 $('#contact-form').hide();

 
  );
 
return false;
);
);
</script>

插入数据的php代码是,

<?php 
require('connection.php');

if($_POST) 

$id = $_POST['regid'];
$name = $_POST['name'];
$contact_no = $_POST['contact_no'];
$email = $_POST['email'];
$requestType = $_POST['requestType'];


    if ($id == '') 
        $sql = "INSERT INTO registration(name,contact_no,email,requestType) VALUES ".
            "('".$name."', '".$contact_no."', '".$email."','".$requestType."')";
     
    $query_result = mysql_query( $sql );
    if(!$query_result ) 
        echo 'Could not enter data: ' . mysql_error();
     else 
        //header("Location: lead-view.php?s=Y");
        $successMsg = 'Record instered successfully';
    
    
    ?>

我想在提交相关潜水点击按钮的表单后将这些 div id 保存为 url。 url 将保存在数据库的“requestType”字段中。请帮忙。

【问题讨论】:

【参考方案1】:

您可以使用 sessionStorage 来传递 requestType 值。 https://www.nczonline.net/blog/2009/07/21/introduction-to-sessionstorage/

在调用 openWin() 时,将 div 值传递给它。例如:

<button type="button" onclick="openWin('abc')" class="modalBtn btn btn-default" id="Supermarkets, Fruits and Vegetables" >Request for Demo</button>

然后将这个值保存在sessionStorage中,如下:

function openWin(div_value) 
  sessionStorage.setItem("registerType", div_value); 
  window.open("register.php");

您可以在 register.php 中检索此变量:

var registerType = sessionStorage.getItem("registerType");

将此值添加到注册表中的隐藏输入控件并保存到数据库。

<input type="hidden" id="requestType">

【讨论】:

但是表单会保存 register.php 点击每个 div 按钮后,相同的 register.php 表单将在新窗口中打开并保存在数据库中

以上是关于如何使用php将上一页可点击的url保存在数据库中?的主要内容,如果未能解决你的问题,请参考以下文章

将数据从 Android 发送到 PHP 服务器

如何将用户重定向到以前的URL在Laravel中注册后

从 PHP URL 保存图像

如何实现在分页的时候,点击下一页记录上一页选中的数据?

如何将旋转图像保存在 iPhone 相册中?

如何使用 PHP 重定向并将链接保存在 SQL 中?