How do I grab the text, not the value of the dropdown list?

Greetings again.

I have the following:

      <?php
	$conn=mysqli_connect("localhost","myuuser","mypass","mydb");
	 // Check connection
	 if (mysqli_connect_errno())
	   {
	   echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}
	$select_query="Select pastorname, ministryname from xphias_clients";
	$select_query = mysqli_query( $conn,$select_query);
	echo "<select name='pastorsname' id='pastorsname' class='form-control'>";
	while ($rc=   mysqli_fetch_array($select_query) )
	{
	   echo "<option value='" .$rc[ministryname] . "'>" . $rc['pastorname'] . "</option>";
	}
	echo "</select>"; ?></td>
     <td><INPUT TYPE="TEXT" class="form-control" NAME="ministriesname" id='ministriesname' SIZE="16"></td>

     //JS
	<script type="text/javascript">
	$(document).ready(function(){
	    $('#pastorsname').change(function(){
	      var ministriesname = $(this).val();
	      $('#ministriesname').val(ministriesname);
	    });
     });
    </script>

Open in new window


On the server side, how do I grab the text of the dropdown list?

In other words, instead of grabbing the value of ministryname, I want to grab the value of pastorname in the following:

 . "'>" . $rc['pastorname'] . "</option>";]

Open in new window


Is it possible?

Thanks in advance
LVL 29
sammySeltzerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
You can set a data attr in the option element to set and get the pastorname as the following example:
echo "<option value='" .$rc[ministryname] . "'  data-pastor=" . $rc['pastorname'] . "></option>";

Open in new window

0
Leonidas DosasCommented:
And if you to get the pastorname in the Jquery script you can work like this:
$(document).ready(function(){
	    $('#pastorsname').change(function(){
	      var ministriesname = $(this).val();
              var pastorName=$(this).data('pastor');
	      $('#ministriesname').val(ministriesname);
	    });
     });

Open in new window

0
sammySeltzerAuthor Commented:
Thank you for your prompt response but my question if I need to grab the text in server side like:

$textval = $_POST["pastorsname"];

how do I ensure that I am getting the text  of <select name="pastorsname"...></select>??

I want to insert that value into the database.

Thanks again
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Olaf DoschkeSoftware DeveloperCommented:
You don't get the displayed text over, unless you make it that extra attribute of the option tags. HTML forms always only send over attributes, mainly the value attribute for each name attribute. So only get the pastorname in $_POST["pastorsname"], if you also put in the pastorsnames into the option values, instead of putting the ministryname in there:

echo "<option value='" .$rc[pastorname] . "'>" . $rc['pastorname'] . "</option>";

Open in new window

instead of
echo "<option value='" .$rc[ministryname] . "'>" . $rc['pastorname'] . "</option>";

Open in new window

Option texts are allowed to differ from the values, but you always only get the values sent over to the server, so there is nothing extra you can pull out. Not sure how data-* attributes would change this, but on the client side you might hook into the submit and change the value to contain the ministry, too.

Though you do have an extra field for the ministriesname already.

Bye, Olaf.
0
sammySeltzerAuthor Commented:
I see.

So, php does not work like classic ASP or ASP.NET because in any of them, you can always grab the text value using TextItem attribute.

There is got to be a way to do this.

What we are trying to accomplish is to select a pastor from dropdown and then the ministry associated with that pastor is automatically populated in the ministry textbox.

If there is a better to do this, someone please let me know
0
Julian HansenCommented:
We are dealing with different concepts here

In other words, instead of grabbing the value of ministryname, I want to grab the value of pastorname in the following:
In any environment on the form values are sent back - not the options they were linked to - for obvious reasons.

So, php does not work like classic ASP or ASP.NET because in any of them, you can always grab the text value using TextItem attribute.
No, ASP does not work the way you think it does - the functionality with respect to forms is the same - only the value is sent back. What happens on the server side is that you can use that value on the server to go and find what option it is linked to.
So in PHP it would look something like this
<?php
$ministryname = isset($_POST['pastorsname']) ? $_POST['pastorsname'] : false;
if ($ministryname ) {
  $query = "Select pastorname from xphias_clients where ministryname ='{$ministryname}'";
  $result = mysqli_query( $conn,$query)
  if ($result) {
    $data= mysqli_fetch_array($result);
    $pastorname = $data['pastorname'];
}

Open in new window

NB: I have used your form field names - which are confusing. You load the ministry name into the value of the options for the select but then call the select field pastorname - I have kept it as is.
0
sammySeltzerAuthor Commented:
Julian,

Thanks for your response.

IN ASP, assuming that I have the following:

               <asp:DropDownList id="Pastorsname"  runat="server">
                  <asp:ListItem Selected="True" Value="St Thomas Church"> Pastor James Baker</asp:ListItem>
                  <asp:ListItem Value="Holy Mary Ministry">Pastor John Doe </asp:ListItem>
               </asp:DropDownList>

Open in new window


This is essentially the same dropdown I have in the php code.

So, in this example, if I wish to grab the value of the dropdown which is the ministryname, I would simply do this:

Pastorsname.SelectedValue

Open in new window


If however, I am trying to get the text, not the value which  in this case is Pastorname, all I have to do is:

<select name which is pastor.Text value  and it would look like this:
Pastorsname.SelectedItem.Text

Open in new window


If I use this for my insert statement, it will always store the value of Pastorname, not ministryname.

What I am saying is that isn't there equivalence of this in php where instead of:

$thepastorname = $_POST['pastorsname'] 

Open in new window


which will always store the value of ministry but something that grabs the pastorname value and store that in the database.

In your solution, assuming that I have this:

Insert into mytable(ministryname, pastorname) values( what will I use here as pastorname) based on your example.

That's where I am a bit confused.
0
Julian HansenCommented:
ASP does the work for you but the process is the same. In ASP the display value is not being returned ASP is looking it up for you based on the form value sent back from the form.

The select in your example returns the ministry name - from that you want to insert the pastorname - you can do this in your query
$ministryname = isset($_POST['pastorsname']) ? $_POST['pastorsname'] : false;
if ($ministryname ) {
   $query = <<< QUERY
INSERT INTO  myTable (ministryname, pastorname) 
 SELECT ministryname, pastorname FROM xphias_clients where ministryname ='{$ministryname}'
QUERY;
  $result = mysqli_query( $conn,$query)
}

Open in new window

0
sammySeltzerAuthor Commented:
Julian, this is a great way of doing it but the issue here is that I have more than two database fields.

In fact, I have 7. Sorry I used two fields in my example.

This is the actual code:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {

    $sql = "INSERT INTO xphias_sermons (pastorname, ministriesname, sermon_name, scripture, video_name, sermon_notes,sermon_date) VALUES (?, ?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);

    $stmt->bind_param('sssssss', $pastorname, $ministriesname, $sermonname, $scriptures, $videoname, $sermonnotes, $sermondate);
}

$pastorname= $_POST['pastorsname'];
$ministriesname= $_POST['ministriesname'];
$sermondate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['sermondate'])));
$sermonname= $_POST['sermonname'];
$scriptures= $_POST['scriptures'];
$videoname= $_POST['videoname'];
$sermonnotes= $_POST['sermonnotes'];

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);

Open in new window

0
Julian HansenCommented:
Why can't you just include those in the Query
$sql = "INSERT INTO xphias_sermons (pastorname, ministriesname, sermon_name, scripture, video_name, sermon_notes,sermon_date) 
SELECT pastorname, ministriesname, ?,?,?,?,?) FROM xphias_clients where ministryname =?";

Open in new window

Now you just bind your variables as you would.

A select statement can include constant values - they don't all have to come from the table - so in this case you pull the pastorname and ministriesname from the table (or you can bind to the ministry name passed back from the form) and the rest you pass in in as bound variables from the post.
0
sammySeltzerAuthor Commented:
No, they are not all from the same table.

I thought I explained this earlier; sorry if I neglected to do so.

xphias_clients has about 5 columns including Ministryname and pastorname.

Then there is another table called xphias_sermons that has 7 fieldnames including ministryname and pastorname.

So, the requirement is that once record has been created in xphias_clients table, create a dynamically populated dropdownlist so user can select pastorname from the dropdown list and once a pastorname is selected, then the ministryname associated with that pastername will automatically populate a textbox.

user then enters values for the renaming 5 form fields, grabs their values from process.php file and insert into the xphias_sermons table.

So, unfortunately, wouldn't work the way you designed it.
0
Julian HansenCommented:
So, unfortunately, wouldn't work the way you designed it.
I don't think you understood my suggestion.

In your INSERT STATEMENT Above where do these values come from
$pastorname, $ministriesname, $sermonname, $scriptures, $videoname, $sermonnotes, $sermondate

Open in new window

Because you are only capturing the POST after the query.

My solution works on the premise that you can do this

SELECT pastorname, ministriename, 'Value 1', 'Value 2', 'Value 3', 'Value 4' FROM xphias_clients

Open in new window

The only values that have to be in table xphias_clients are the first two (which you have already confirmed).
The rest of the values I am assuming come from the variables in the code snippet above - although it is not clear where they come from.
What you could do then is
$sql = <<< QUERY
INSERT INTO xphias_sermons (
		pastorname, 
		ministriesname, 
		sermon_name, 
		scripture, 
		video_name, 
		sermon_notes,
		sermon_date
	) 
SELECT ministryname, pastorname, ?, ?, ?, ?, ? FROM xphias_clients WHERE ministryname = ?
QUERY;

$stmt = $conn->prepare($sql);

$stmt->bind_param('ssssss', 
	$sermonname, 
	$scriptures, 
	$videoname, 
	$sermonnotes, 
	$sermondate, 
	$ministriesname
);

Open in new window

I cannot give you a complete code sample yet as I do not know where these come from
	$sermonname, 
	$scriptures, 
	$videoname, 
	$sermonnotes, 
	$sermondate, 
	$ministriesname

Open in new window

Can you fill in those blanks and we can take it from there.

Bottom line - you can use a SELECT to INSERT that partially pulls data from a table and partially provides constant values for the remaining fields. That is what I am basing my solution on.
0
sammySeltzerAuthor Commented:
Julian,

Thanks again for your help.

Let  me lay the structures for both tables.

xphias_clients:
clientId,
pastorname,
ministryname,
xstartdate,
clientname,
 url

xphias_sermons
sermon_id
sermonname,
scriptures,
videoname,
sermonnotes,
sermondate,
ministriesname

So to answer your question, these:
1:      $sermonname,
2:      $scriptures,
3:      $videoname,
4:      $sermonnotes,
5:      $sermondate,
6:      $ministriesname
are from xphias_sermons.

I am trying to get the values of pastorname from xphias_clients and based on what value of pastorname is selected, pull the associated ministryname from xphias_clients table and then with those values along with the following fieldnames from xphias_sermons table, insert them into xphias_sermons table.

Maybe the table design is flawed but those are the structures.

So, I guess my question then is that assuming that's a workable solution, how do I use the pastorsname form field?

In other words, should still be:

$pastorsname= $_POST['pastorsname'];

Open in new window

?

Thanks again sir for your help.
0
sammySeltzerAuthor Commented:
I tried inserting records based on the information you posted and based on using
$pastorname = $_POST["pastorsname"}];

Open in new window

, it inserted all the records correctly except that it inserted ministry information on both pastorname and ministry name. Not good.
0
sammySeltzerAuthor Commented:
I tried inserting records based on the information you posted and based on using
$pastorname = $_POST["pastorsname"}];

Open in new window

, it inserted all the records correctly except that it inserted ministry information on both pastorname and ministry name. Not good.
0
Julian HansenCommented:
I need some clarity - we started with a form - I assumed that you were sending data from a form that was going to be sent to a table. In your last post you said that you are sourcing the variables I asked about from another table.

Where does the form come into this?
0
sammySeltzerAuthor Commented:
Julian,

If you look at my original post, the code I posted was part of HTML markup.

At the time, I was more concerned about how to grab <SELECT...>...</SELECT>  label or text and insert that into the database instead of the SELECT VALUE.

So, what I am saying In my last post is that there are more form fields besides the <SELECT> markup and the ministryname <INPUT text.

So, all those fields you asked me about what table they belong to, are part of the markup that need to be sent to processing page along with pastorname and ministryname.

I am am clear. Thank you.
0
Julian HansenCommented:
That is what I was trying to establish - when I asked where those variables came from I expected the answer to be the form but you responded they came from another table. Your script also shows those variables being filled from $_POST AFTER the query - so I am confused.

If the variables are being filled from the form then my original solution stands - simply insert them as constants into the query.

Can you post your entire script so I can see the context.
0
Olaf DoschkeSoftware DeveloperCommented:
What we are trying to accomplish is to select a pastor from the drop-down and then the ministry associated with that pastor is automatically populated in the ministry textbox.

Well, you got that answer in the first answer from Leonidas Dosas by adding a new data-attribute. Instead of data-pastor you could just add data-ministry, put the ministry in there and on the selection of a pastor grab that data attribute value and put it into the ministry text input.

In fact, you don't even need that, because here you don't act on the server side, you act on the client side at the event a pastor is picked and populate the ministry. That's done by client-side javascript (jQuery) and not server side; jQuery can both pick out the data-ministry or the options text value. For the server side it's sufficient the client side js has forwarded the ministry to the ministry input, you get two matching values. Regarding sanitizing user input, you'd rather only use the pastor input and lookup what ministry he is in via your database. As anyone knowing the action of an HTML form and the form input names can mimic a form submission to that action script. So any related values should only be put into a form for the informational purpose of the user. If you only display the ministry for the user picking the pastor, he'll see if that fits his expectation or he simple gets that information, to you the pastor alone is sufficient to know the ministry, so you should do your form processing accordingly, even though you already output that knowledge about ministry-pastor relationships into your form, you always have to expect some bots misusing your forms.

Even if your data isn't valuable, any website hackable by bot scripts adds resources for spam mailing and database storage to hackers.
 and you, therefore, don't expect any hacking. Even, when you want to close your eyes to such aspects, you better design forms and form processing in a way you don't depend on intact information coming back to you. When you output some relation don't expect it to come back. But as far as you can rely on the detail data of the pastor (I assume it's one ministry, one or multiple pastors) you can reconstruct the ministry yourself, so there is no need to ask for it, that input field should merely be an output field informing the user which ministry he picked by picking a pastor, but your form processing script only needs to know the pastor.

Bye, Olaf.
0
Olaf DoschkeSoftware DeveloperCommented:
Another view on this from the side of how to design the form ideally:
1. You only render a select with ministry options
2. On selection from this select, you make an ajax request for pastors and render a new select with pastors of that ministry.
3. Your submit only submits the pastor as the ministry he is related to is unmistaken.

Unless the situation is different and pastors may be associated with several ministries, so there is an n:m relationship. then you need both information as feedback on submit but would cross check them with your data anyway.

Bye, Olaf.
0
sammySeltzerAuthor Commented:
Hi Julian,

Here is the entire code:

//Markup:

      <table id="dataTable">
      <tbody>
    <tr>
     <td>
      <?php
	$conn=mysqli_connect("localhost","myusername","mypassword","mydb");
	 // Check connection
	 if (mysqli_connect_errno())
	   {
	   echo "Failed to connect to MySQL: " . mysqli_connect_error();
	 }
	$select_query="Select pastorname, ministryname from xphias_clients";
	$select_query = mysqli_query( $conn,$select_query);
	echo "<select name='pastorsname' id='pastorsname' class='form-control'>";
	echo "<option value=' '></option>";
	while ($rc=   mysqli_fetch_array($select_query) )
	{
	   echo "<option value='" .$rc['ministryname'] . "'>" . $rc['pastorname'] . "</option>";
	}
	echo "</select>"; ?></td>
     <td><INPUT TYPE="TEXT" class="form-control" NAME="ministriesname" id="ministriesname" SIZE="16"></td>
     <td><INPUT TYPE="TEXT" class="form-control" NAME="sermondate" SIZE="10"></td>
     <td><INPUT TYPE="TEXT" class="form-control" NAME="sermonname" SIZE="16"></td>
     <td><INPUT INPUT="TEXT" class="form-control" NAME="scriptures" SIZE="16"></td>
     <td><INPUT INPUT="TEXT" class="form-control" NAME="videoname" SIZE="10"></td>
     <td><INPUT INPUT="TEXT" class="form-control" NAME="sermonnotes" SIZE="16"></td>
    </tr>
   </table>
   <table>
    <tr>
     <td><INPUT type="file" class="form-control" name="fileToUpload" /></td>
     <td><INPUT TYPE="submit" value="Save" SIZE="14" name="upd"></td>
    </tr>
  </tbody>
 </table>

 //JS
    <script type="text/javascript">
      $(document).ready(function(){
      $('#pastorsname').change(function(){
      var ministriesname = $(this).val();
      $('#ministriesname').val(pastorsname);
     });
    });
   </script>

Open in new window


   //process.php
<?php

 $conn=mysqli_connect("localhost","myusername","mypassword","mydb");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

  $target_dir = "video_upload/";

  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

 $upd= $_POST['upd'];

  if($upd)
  {
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

  if($imageFileType != "mp4" && $imageFileType != "mov" && $imageFileType != "mp2")
  {
      echo "File Format Not Suppoted";
  }
else
{

if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {

    $sql = "INSERT INTO xphias_sermons (pastorname, ministriesname, sermon_name, scripture, video_name, sermon_notes,sermon_date)VALUES (?, ?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);

    $stmt->bind_param('sssssss', $pastorname, $ministriesname, $sermonname, $scriptures, $videoname, $sermonnotes, $sermondate);
}

$pastorname = $_POST['pastorsname'];
$ministriesname= $_POST['ministriesname'];
$sermondate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['sermondate'])));
$sermonname= $_POST['sermonname'];
$scriptures= $_POST['scriptures'];
$videoname= $_POST['videoname'];
$sermonnotes= $_POST['sermonnotes'];

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);


//Execute statement
$stmt->execute();

if ($stmt->errno) {
  echo "FAILURE!!! " . $stmt->error;
}
else echo "Added {$stmt->affected_rows} rows";

$stmt->close();

}

}
?>		   

Open in new window


Thank you.


Olaf, I am trying so hard to follow your suggestions but I am completely lost.

Maybe an example will help?
0
Julian HansenCommented:
We are nearly there - I just need to understand one more thing
Lines 28-43
You are creating an INSERT query with these variables
$pastorname, $ministriesname, $sermonname, $scriptures, $videoname, $sermonnotes, $sermondate

Open in new window

But you are only extracting those from the $_POST AFTER the query ... Why?
0
sammySeltzerAuthor Commented:
If I understand you correctly, your question is why I am using these after insert statement?


$pastorname = $_POST['pastorsname'];
$ministriesname= $_POST['ministriesname'];
$sermondate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['sermondate'])));
$sermonname= $_POST['sermonname'];
$scriptures= $_POST['scriptures'];
$videoname= $_POST['videoname'];
$sermonnotes= $_POST['sermonnotes'];

Open in new window



If that's your question, I didn't think that it matters whether you declare them before the insert statement or after.
0
Julian HansenCommented:
I didn't think that it matters whether you declare them before the insert statement or after.
No, you are right - I did not scroll down - you are binding them before the execute which is fine.

	echo "<select name='pastorsname' id='pastorsname' class='form-control'>";
	echo "<option value=' '></option>";
	while ($rc=   mysqli_fetch_array($select_query) )
	{
	   echo "<option value='" .$rc['ministryname'] . "'>" . $rc['pastorname'] . "</option>";
	}
	echo "</select>"; ?></td>
     <td><INPUT TYPE="TEXT" class="form-control" NAME="ministriesname" id="ministriesname" SIZE="16"></td>

Open in new window


You have a <select> that has as values 'ministryname' and then you are asking for a ministriesname in an input - I am assuming these are not related - if they are can you explain.

In the meantime here is the code as I would do it
//process.php
<?php
$conn=mysqli_connect("localhost","myusername","mypassword","mydb");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$target_dir = "video_upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$upd= $_POST['upd'];
if($upd) {
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  if($imageFileType != "mp4" && $imageFileType != "mov" && $imageFileType != "mp2") {
    echo "File Format Not Suppoted";
  }
  else {
    $pastorname = $_POST['pastorsname'];
    $ministriesname= $_POST['ministriesname'];
    $sermondate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['sermondate'])));
    $sermonname= $_POST['sermonname'];
    $scriptures= $_POST['scriptures'];
    $videoname= $_POST['videoname'];
    $sermonnotes= $_POST['sermonnotes'];

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {

      $sql = <<< QUERY
INSERT INTO xphias_sermons (
  pastorname, 
  ministriesname, 
  sermon_name, 
  scripture, 
  video_name, 
  sermon_notes,
  sermon_date
  )
SELECT 
  pastorname,?,?,?,?,?,? 
FROM 
  xphias_clients 
WHERE 
  ministryname=?
QUERY;

      $stmt = $conn->prepare($sql);
      $stmt->bind_param('sssssss', $ministriesname, $sermonname, $scriptures, $videoname, $sermonnotes, $sermondate, $pastorname);
    }
    
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);

    //Execute statement
    $stmt->execute();

    if ($stmt->errno) {
      echo "FAILURE!!! " . $stmt->error;
    }
    else { 
      echo "Added {$stmt->affected_rows} rows";
    }

    $stmt->close();
  }
}

Open in new window

0
Julian HansenCommented:
Bear in mind that in your code your <select name="pastorsname"> has ministryname as the values - it is a bit confusing - I have kept as is but the code does not read well with this setup.
0
sammySeltzerAuthor Commented:
You are correct about the <SELecT... setup.

That's precisely what I have been trying to explain all along.

Here is what the specs says:


There are two tables as you and I have noted earlier.

User must first create a record in the xphias_clients table to store pastorname and ministryname and three other field names.

As I understand it, there can only be one ministry per one pastor like 1:1 situation.

Once a record is created in xphias_clients table, you can now create a record for xphias_sermons table.

To do so, pull all the pastorsnames from xphias_clients table and populate a dropdown withpastorname values.

Once that is done, create an input textbox for ministryname
<inputtype="text" name="ministryname" values="<?php echo $ministryname; ?>">

Open in new window


Now, the way it is supposed to work is that when you select a value for pastorsname <SELECT..>...</SELECT>, the value of ministryname associated with that pastorname is automatically stored in $ministryname in the textbox.

I hope this makes sense.

My original approach was to create a
SELECT ministryname from xphias_clients where pastorname = $_GET["pastorsname"];

Open in new window

I tried using POST as well.

So, if you selects a value for pastorsname from the dropdown, the value of ministryname is automatically inserted into the textbox for $ministryname.

However, I could not get this to work. That's why I came up with the current code.

Sorry for long text but if you think my original approach would work, then what did I miss from making it work?
0
sammySeltzerAuthor Commented:
Your current solution actually worked!

My concern is that when you say the code does not read well the setup I have, what do you mean?

Is it going to produce inconsistent results/
0
Julian HansenCommented:
Ok - in the first response from Leonidas (https://www.experts-exchange.com/questions/29068921/How-do-I-grab-the-text-not-the-value-of-the-dropdown-list.html#a42371316) he gives you a way to populate the ministries text box from the <select>

You responded by saying you wanted to do that on the sever - which is how we ended up here.

My advice is go back to Leonidas first post and implement that - make your <select> (names and values) the  pastorname. Add the ministry names as data attributes on the options then use Leonidas code to populate the ministry text input when the select changes - then everything should work as expected with your existing code - no need to change anything.
0
Julian HansenCommented:
My concern is that when you say the code does not read well the setup I have, what do you mean?

Is it going to produce inconsistent results/
No I meant that you have a variable called pastorname that stores ministryname - causes confusion to those who read the code - PHP interpreter does not care about English semantics so it will work fine.
0
sammySeltzerAuthor Commented:
When I use Leonidas code and use this as is:


   //process.php
<?php

 $conn=mysqli_connect("localhost","myusername","mypassword","mydb");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

  $target_dir = "video_upload/";

  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

 $upd= $_POST['upd'];

  if($upd)
  {
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

  if($imageFileType != "mp4" && $imageFileType != "mov" && $imageFileType != "mp2")
  {
      echo "File Format Not Suppoted";
  }
else
{

if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {

    $sql = "INSERT INTO xphias_sermons (pastorname, ministriesname, sermon_name, scripture, video_name, sermon_notes,sermon_date)VALUES (?, ?, ?, ?, ?, ?, ?)";
    $stmt = $conn->prepare($sql);

    $stmt->bind_param('sssssss', $pastorname, $ministriesname, $sermonname, $scriptures, $videoname, $sermonnotes, $sermondate);
}

$pastorname = $_POST['pastorsname'];
$ministriesname= $_POST['ministriesname'];
$sermondate = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $_POST['sermondate'])));
$sermonname= $_POST['sermonname'];
$scriptures= $_POST['scriptures'];
$videoname= $_POST['videoname'];
$sermonnotes= $_POST['sermonnotes'];

move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$target_file);


//Execute statement
$stmt->execute();

if ($stmt->errno) {
  echo "FAILURE!!! " . $stmt->error;
}
else echo "Added {$stmt->affected_rows} rows";

$stmt->close();

}

}
?>		   

Open in new window



It adds ministriesname's value into both the pastorsname column and ministriesname column.


Unless something is missing, I will just use your code for now.
0
Julian HansenCommented:
For Leonidas code you would need to do this
//Markup:
<table id="dataTable">
  <tbody>
    <tr>
     <td>
      <?php
	$conn=mysqli_connect("localhost","myusername","mypassword","mydb");
	 // Check connection
	 if (mysqli_connect_errno())
	   {
	   echo "Failed to connect to MySQL: " . mysqli_connect_error();
	 }
	$select_query="Select pastorname, ministryname from xphias_clients";
	$select_query = mysqli_query( $conn,$select_query);
	echo "<select name='pastorsname' id='pastorsname' class='form-control'>";
	echo "<option value=' '></option>";
	while ($rc=mysqli_fetch_array($select_query) )
	{
	   echo <<< OPTION
		<option value="{$rc['pastorname']}" data-ministry="$rc['ministryname']">{$rc['pastorname']}</option>
OPTION;
	}
	echo "</select>"; ?></td>
     <td><INPUT TYPE="TEXT" class="form-control" NAME="ministriesname" id="ministriesname" SIZE="16"></td>
     <td><INPUT TYPE="TEXT" class="form-control" NAME="sermondate" SIZE="10"></td>
     <td><INPUT TYPE="TEXT" class="form-control" NAME="sermonname" SIZE="16"></td>
     <td><INPUT INPUT="TEXT" class="form-control" NAME="scriptures" SIZE="16"></td>
     <td><INPUT INPUT="TEXT" class="form-control" NAME="videoname" SIZE="10"></td>
     <td><INPUT INPUT="TEXT" class="form-control" NAME="sermonnotes" SIZE="16"></td>
    </tr>
   </table>
   <table>
    <tr>
     <td><INPUT type="file" class="form-control" name="fileToUpload" /></td>
     <td><INPUT TYPE="submit" value="Save" SIZE="14" name="upd"></td>
    </tr>
  </tbody>
 </table>

 //JS
<script type="text/javascript">
$(function(){
	$('#pastorsname').change(function(){
		var ministriesname = $(':selected', this).data('ministry');
		$('#ministriesname').val(ministriesname);
	});
});
</script>

Open in new window

0
sammySeltzerAuthor Commented:
Yea, I left out some things. Sorry about that.

However, I am getting an error on this line:

<option value="{$rc['pastorname']}" data-ministry="$rc['ministryname']">{$rc['pastorname']}</option>

Open in new window


The error says, Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in ...
0
Julian HansenCommented:
Missing { } around $rc['ministryname'] in the string should be
<option value="{$rc['pastorname']}" data-ministry="{$rc['ministryname']}">{$rc['pastorname']}</option>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sammySeltzerAuthor Commented:
Many thanks Julian for your incredible patience and assistance.

Since I am still having problem getting this solution to work, I will just accept your own version of the solution.
0
Julian HansenCommented:
Please feel free to post back if you are having issues and lets see if we can help you solve them.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.