Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8341779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:35:10+00:00 2026-06-09T05:35:10+00:00

I am trying to send a value from my test.php to my backgroundScript.php through

  • 0

I am trying to send a value from my test.php to my backgroundScript.php through the $_POST method and when print out the value that is being sent i get the correct value but when i go to my backgroundScript file to see if the value was sent – nothing gets printed out, it equals null ——————————–test.php file – main code

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH->query('SELECT first FROM contacts'); 
//set fetch mode
$FNresult->setFetchMode(PDO::FETCH_ASSOC);

$dropdown = "<select name='contacts' id='contacts' >";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option  value='{$row['first']}'>{$row['first']}</option>";
 // echo getLN();

}

$dropdown .= "\r\n</select>";

echo $dropdown;


//}
/*
//                  Get last name

function getLN(){
    $query = "SELECT last FROM contacts";
    $LNresult=mysql_query($query);

    $last;
    while($row = mysql_fetch_assoc($LNresult)) {

        $last = "{$row['last']}";

    }
    echo $last;
}//end getLN
*/

$DBH = null; 
?>


<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"></script>   
<!-- javascript on client-side -->
<script type="text/javascript">  

var dropdown = $('#contacts');

document.write(dropdown.val());

dropdown.bind('change', function(){

    $.post('backgroundScript.php', 
        { 
            first: dropdown.val()

        },
        function(response) {
            $('#first').val(response.first);
            $('#last').val(response.last);
            $('#phone').val(response.phone);
            // Repeat for all of your form fields
        },
        'json'
    );

});

</script>

<form action="insert.php" method="post">
First Name: <input type="text" id="first"  ><br>
Last Name: <input type="text" id="last"><br>
Phone: <input type="text" id="phone"><br>
Mobile: <input type="text" id="mobile"><br>
Fax: <input type="text" id="fax"><br>
E-mail: <input type="text" id="email"><br>
Web: <input type="text" id="web"><br>
<input type="Submit">
</form>

backgroundScript.php ——————————————- where value is sent

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  

$first= $_POST['first'];
print_r("print value: $first");
//$first = "david";
$sth = $DBH->prepare('SELECT *
    FROM contacts
    WHERE first = :first');
$sth->bindParam(':first', $first, PDO::PARAM_STR);
$sth->execute();

$row = $sth->fetch();


$returnArray = array( 
'first' => $row["first"],
'last' => $row["last"], 
'phone' => $row["phone"], 
);
//print_r("After pureeing fruit, the colour is: $returnArray");
/*echo "<pre>";
print_r($returnArray);
echo "</pre>";
exit;*/


// background script

// retrieve data based on $_POST variable, set to $returnArray
/*while($row = $sth->fetch(PDO::FETCH_ASSOC)){
  // do something with row

}
$returnArray = array( 
'first' => $row["first"], 
);


echo "<pre>";
print_r($returnArray);
echo "</pre>";
exit;
*/



/****************************
 * the structure of returnArray should look something like
     array(
         'first' => firstName,
         'last' => lastName,

     )*/
    // echo json_encode(array('first' => "hello", 'last' => "Other value"));
//echo json_encode($returnArray);
$DBH = null; 

############################ UPDATE TO TEST.PHP

try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

  //$DBH->prepare('SELECT first FROM contacts');
}  
catch(PDOException $e) { 
    echo "I'm sorry, I'm afraid I can't do that.";  
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
}  
//get query
$FNresult=$DBH->query('SELECT first FROM contacts'); 
//set fetch mode
$FNresult->setFetchMode(PDO::FETCH_ASSOC);


//}
/*
//                  Get last name

function getLN(){
    $query = "SELECT last FROM contacts";
    $LNresult=mysql_query($query);

    $last;
    while($row = mysql_fetch_assoc($LNresult)) {

        $last = "{$row['last']}";

    }
    echo $last;
}//end getLN
*/

$DBH = null; 
?>


<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"></script>   
<!-- javascript on client-side -->
<script type="text/javascript">  

var dropdown = $('#contacts');

document.write(dropdown.val());

dropdown.bind('change', function(){

    $.post('backgroundScript.php', 
        { 
            first: dropdown.val()

        },
        function(response) {
            $('#first').val(response.first);
            $('#last').val(response.last);
            $('#phone').val(response.phone);
            // Repeat for all of your form fields
        },
        'json'
    );

});

</script>

<form action="insert.php" method="post">
<?php

$dropdown = "<select name='contacts' id='contacts' >";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option  value='{$row['first']}'>{$row['first']}</option>";
 // echo getLN();

}

$dropdown .= "\r\n</select>";



echo $dropdown;
?>

First Name: <input type="text" name="first" id="first" ><br>
Last Name: <input type="text" id="last"><br>
Phone: <input type="text" id="phone"><br>
Mobile: <input type="text" id="mobile"><br>
Fax: <input type="text" id="fax"><br>
E-mail: <input type="text" id="email"><br>
Web: <input type="text" id="web"><br>
<input type="Submit">
</form>

Now i get a undefined in javascript when i try to print out value of dropdown

######################################## UPDATE

<form action="backgroundScript.php" method="post">

<script type="text/javascript"
     src="http://code.jquery.com/jquery-latest.min.js"></script>   
<!-- javascript on client-side -->
<script type="text/javascript">  

var dropdown = $('#contacts');

document.write(dropdown.val());

dropdown.bind('change', function(){

    $.post('backgroundScript.php', 
        { 
            first: dropdown.val()

        },
        function(response) {
            $('#first').val(response.first);
            $('#last').val(response.last);
            $('#phone').val(response.phone);
            // Repeat for all of your form fields
        },
        'json'
    );

});

</script>


<?php

$dropdown = "<select name='contacts' id='contacts' >";

while($row =$FNresult->fetch()) {

  $dropdown .= "\r\n<option  value='{$row['first']}'>{$row['first']}</option>";
}
$dropdown .= "\r\n</select>";

echo $dropdown;
?>

First Name: <input type="text" name="first" id="first" ><br>
Last Name: <input type="text" name="last" id="last"><br>
Phone: <input type="text"  name="phone" id="phone"><br>
Mobile: <input type="text" id="mobile"><br>
Fax: <input type="text" id="fax"><br>
E-mail: <input type="text" id="email"><br>
Web: <input type="text" id="web"><br>
<input type="Submit">
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:35:12+00:00Added an answer on June 9, 2026 at 5:35 am

    POST uses name instead of id to identify parameters. Use name on your fields – or use both.

    <input type="text" name="first" id="first" >
    

    Javascript can make do of IDs nicely with the whole getElementByID but POST and GET data needs the name to be used.

    Edit: You also need to make sure that the <select....> statement falls inside the <form> and </form> tags, otherwise it isn’t sent as part of that form.

    Stick this line:

    <form action="backgroundScript.php" method="post">
    

    above these lines:

    <script type="text/javascript"
         src="http://code.jquery.com/jquery-latest.min.js"></script>   
    <!-- javascript on client-side -->
    <script type="text/javascript">  
    

    Edit 2: Also, as Herpa points out, your <form action=''> is set to insert.php and not backgroundScript.php. You need to change that as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send a value to the appDelegate from one UIViewController, so that
am trying send an array to php file using $_POST ,still always getting an
Effectively I'm trying to send some template emails so that I can test a
I'm trying to get a value from a netcat connection started at a php
I'm trying to send a URL to my server that uses values from edittext
I'm trying to send POST data to a test.php file, which handles POST data
I am trying to send the clicked value of a dropdown list to my
I am trying to get jquery's .ajax() to send values sent to a form
Trying to send a complex type between two systems that have the same code
Hi I'm trying to figure out how to echo back if the value entered

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.