简体   繁体   中英

pass php variables from one form to be submitted to database in another

I have a pop up box which checks if the user is signed in or not. If he is, I'm echoing out a small form which the user will press a button and it will submit to the DB. The variables are displayed on the popup but when pressed submit, they do not pass to the submit php file.

$add_wish = "<form action='memWishList.php' method='post' id='memWishList'>
    <h3>Add this item to your Wish List?</h3><br>
    <input type='hidden' name='title' value='".$title."'>".$title."</input><br>
    <input type='hidden' name='link' value='".$link."'></input><br>
    <input type='submit' name='submit' value='Add'/><button id='cancel'>
      Cancel</button>
</form>";
    echo $add_wish;

I want to pass the values title and link to be submitted to the DB. Here's my memWishList.php file:

if (isset($_POST['submit'])){
  //get member id
  $title = mysqli_real_escape_string($_POST['title']);
  $link = mysqli_real_escape_string($_POST['link']);
  $mysql = "INSERT INTO wish_list (memNum, title, link, date) VALUES ('$memnum', \ 
      '$title', '$link', now())";
    $myquery = mysqli_query($mysqli_connect, $mysql);}

Doing it this way, I only get the member id and the date inserted, not the title and the link. What's the problem? The reason why I'm echoing out this form is there's an if/else statement for logged in users and non logged in. Would be much easier to do it in html but can't...

DB: memnum(varchar), title(longtext), link(longtext), date(date). I have other tables where long links and titles are inserted just fine as longtext. They're coming from rss feeds.

please check documentation : mysqli_real_escape_string function expect the string as 2nd parameter if you use a procedural approach. It could be ie:

$link = mysqli_real_escape_string($mysqli_connect, $_POST['link']);

You have some markup errors. Your hidden input tags should look like:

<input type='hidden' name='link' value="<?php echo $link ?>">

Update your HTML file to look like this and all of the values will be sent to the $_POST variable:

<form action='memWishList.php' method='post' id='memWishList'>
    <h3>Add this item to your Wish List?</h3><br>
    <input type='hidden' name='title' value="<?php echo $title ?>"><?php echo $title ?><br>
    <input type='hidden' name='link' value="<?php echo $link ?>"><br>
    <input type='submit' name='submit' value='Add'/><button id='cancel'>Cancel</button>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM