1

I am trying to bring a value from a form in page1.php (form to add data to DB) to page2.php (form to add IMG to DB). I read a whole bunch of pages about how to do that but still can't figure it out.

<form name="formBrokerTour" class="login-form-addproperty" method="post"
  action="insertPropertyInfoWITHgeocode.php" onsubmit="return validateForm()">

And then I would need to add several fields, address, city, zip code state. Here one out of 4:

<td  width="125" class="formLable">Address</td>
<td width="269" class="formSquare">
   <input type="text" class="general_input" 
     name="address" id="nameText" width="269" >
</td>

page2.php (relevant part) - as you see, if I write an address, that address is addded in the database, so we don't need to worry about sql being wrong. If I try to pass info from page 1 to page 2 either by session (since I have username in place to add info in database) or post/get, address is not added. Interestingly, username is being added.

$username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
$address = "123 abec st"; 

$query = "
  INSERT INTO my_image
  (name, size, type, file_path, username, address) 
  VALUES(?,?,?,?,?,?)";
$conn = $db->prepare($query);
if ($conn == TRUE) {
    $conn->bind_param("sissss", $myfile, $fileSize, $fileType, $path, $username,  $address);
    if (!$conn->execute()) {
      echo 'error insert';
    } else {
      echo 'Success!<br/>';
      echo '<img src="' . DISPLAY_PATH . $myfile . '"/>';
    }
}

I tried to echo $_session, etc. as we see in other questions, but here is the problem of a new computer person... where do I add this info? I have added it in session_start on the top, just before SQL query and it didn't accept it inside the query.

page1.php has submit button with DB and geocoding stuff. It sends to page3.php where it asks if user wants to add images. If yes, page2.php, if not index. I am getting all confused with passing info.

2
  • I'm not clear on the exact issue, but you can retrieve POSTed data by accessing the $_POST superglobal. Commented Sep 3, 2013 at 17:48
  • I need to capture values of address, city, state, zipcode (all in the page1.php - with table1 for data stuff ) to page2.php (with table2 for image stuff) where user can add images to DB. I do not want user to add address again because it will not be the same address... Hope it's better explained. There is also page3.php where user has a question whether to add image or not. Commented Sep 3, 2013 at 17:52

4 Answers 4

3

All of the fields from your form are in $_POST. You would extract the address with

$address = $_POST['address'];

If you were passing the variables with a GET form action or via the URL, you would use $_GET.

I'd really suggest looking at tutorial sites or a PHP book, as this is one of the basic things you would learn.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes, I looked at the tutorials... But i can't find one that has the whole picture. Like how to pass values thru several pages with submit button. I will go thru all the answers now. Thank you.
If you want to pass it from 1 to 2 to 3, then on 2 you would save the $_POST variables in a session, or add it as a hidden input on the page 2 form, where you can then get it out of $_POST again on Page 3.
OK, I had tried the $_POST[name_field], but what about the previous page? Do I need to send a header ('Location: some_file.php?variable_name='.$_POST['Some_variable']). I read the tutorials, but they are fragmented. Put it the header or session? Session for me is the same block of code where I have the username stuff, right? Thank you for patience.
You can do that if you want to store it in the $_GET. Personally, I'd go with either the $_SESSION or hidden form fields route. Once you process the POST, you can store it in the session with $_SESSION['value'] = $value;
Session was my first route, then Get/POST. Let's pick session. In the page1.php, I have a block with php code on top for the username. Do I add the $_SESSION['address']=$address there, before the form and submit button?
|
1

Because you're using ? instead of variables in your query, the correct usage of bindParam is like $conn->bindParam(number_of_param, the_value); where number_of_param is the number of the ? you are replacing.

You can also pass an array of parameters to the $conn->execute() call as shown below to replace all the ?'s at once:

        $conn = $db->prepare("INSERT INTO my_image(name, size, type, file_path, username, address) VALUES(?,?,?,?,?,?)");
        if ($conn == TRUE) {
            $result = $conn->execute(array($myfile, $fileSize, $fileType, $path, $username,  $address));
            if (!$result) {
                echo 'error insert';
            } else {
                echo 'Success!<br/>';
                echo '<img src="' . DISPLAY_PATH . $myfile . '"/>';
            }
        }

side note: I would recommend using the PDO::EXCEPTION style of error handling and surrounding your database code with a try {} catch block, as it can help with standardizing error handling, but this is completely up to you.

2 Comments

But I am not getting the value of address from other pages. I am new to PHP (and almost ALL the tutorials are with old musql, a bit with sqli, almost nothing with PDO).
Ah I apologise, I think I've solved the wrong problem here, the variable you want then, as others have said, is $_POST
0

use

$_POST['form input name']

so

VALUE ($_POST['myfile'], $_POST['fileSize'],ect.)

Comments

0

For those struggling with the same question: I added this in all related files

<?php session_start();
$_SESSION['address']=$address;
$_SESSION['city']=$city;
$_SESSION['zip_code']=$zip_code;
?>

I added this before the query:

$username = htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
$address= htmlentities($_SESSION['address']);
$city= htmlentities($_SESSION['city']);
$zip_code= htmlentities($_SESSION['zip_code']);

Then the query:

 $query =
              "INSERT INTO my_image(name, size, type, file_path, username, address, city, zip_code) VALUES(?,?,?,?,?,?,?,?)";
            $conn = $db->prepare($query);
            if ($conn == TRUE) {
                $conn->bind_param("sissssss", $myfile, $fileSize, $fileType, $path, $username, $address, $city, $zip_code);
.......etc.

There may be a little bit of magic, I mean, maybe some steps aren't needed. It's working after trying for a long long time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.