Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a form that upon submit calls a php script to create a drop down list based on a database query. I then need to be able to select an item in the dropdown list, hit a button and have a form populated with the results.

My form is set up like this: with one submit button, and another button type that calls a javascript function.

<form name="searchForm" action="newCustomerSearchform.php" method="post">
 <label><span></span> <input type="text" name="searchDB" /></label>
 <button type="submit" name="btnSearch" value="Search"  id="btnSearch" onclick="this.form.action">Search</button></label>

<?php
  if (isset($_SESSION['names'])){
        echo  '<select  id ="customerDD" name="customerDD" class="customers">';

        foreach ($_SESSION['names'] as $option => $value) {
                echo  '<option value='.$value['ID'].'>'.$value['First_Name'].' '.$value['Surname'].'</option>';
}
        echo  '</select>';
}
if (isset($_SESSION['addnames'])){
        echo 'set';
       echo $_SESSION['addnames'];
  }
?>
 <button type="button" name="btnAdd" value="add" id="btnAdd" onclick=addcustomer();> Add</button>
</form>

The second button calls:

<script type="text/javascript">
var temp = null;
function addcustomer()
{
temp = $("#customerDD").val();
$.post('newCustomerAdd.php', {variable: temp});
}
</script>

When I test this using

document.write(test);

it displayed the correct value of what I curentlly had selected in the drop down.

newCustomerAdd.pp contains a query to my database using the variable from the javascript function and then saves this in a session variable which i use to display on the main page.

<?php
include 'newCustomer.php';
connect('final');

    $temp = $_POST['temp'];
    $temp = htmlspecialchars($temp); // stop HTML charkacters
    $temp= mysql_real_escape_string($temp); //stop SQL injection
    $query = "SELECT * FROM customer WHERE ID = '$temp";
    $data = mysql_query($query) or die(mysql_error());


    $Customers = array();
    $colNames = array();
    while($row = mysql_fetch_assoc($data)){// puts data from database into array, loops until no more

    $Customers[] = $row;

        }
         $anymatches = mysql_num_rows($data); //checks if the querys returned any results
        if ($anymatches != 0) {
        $_SESSION['addnames']=$Customers;

    $colNames = array_keys(reset($Customers));




    }

            if ($anymatches == 0) 
                    { 
                        echo "Sorry, but we can not find an entry to match your query<br><br>"; 
                    } 




header("location: newCustomer.php");
?>

I then test if this variable is set in the form and echo the variable, but nothing is being produced.

It seems like there is something wrong with my Jquery, as the variable is not getting posted to the php script.

share|improve this question
 
I don't see session_start() at the top of the pages that are using the $_SESSION super global. It's imperative that you use it. –  crush Jun 14 at 18:03
 
$temp = $_POST['temp']; $temp = htmlspecialchars($searchtext); // stop HTML charkacters - and what is $searchtext? And many more... –  u_mulder Jun 14 at 18:04
 
session start is included in a header file that is required by all pages. I forgot to change them when i copied from my other file. However changing them didn't resolve the issue –  Dan Barr Jun 14 at 18:12
 
Check if the session variables are defined first using isset(), fi they're not, the next most likely cause is that you're sending headers before the call to session_start(). –  Rafael Jun 14 at 18:24
 
@Dan Barr:-hey den i think you are not getting value on this portion if ($anymatches != 0) { $_SESSION['addnames']=$Customers; $colNames = array_keys(reset($Customers)); }is it so. –  Ankur Saxena Jun 14 at 18:30
show 2 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.