Select the value of a select

1

Hi, how are you all concerned about a select? It happens that I want one of the values of them example:

<select  id="test">
                <option value="100" selected>Precio1</option>
               <option value="200" selected>Precio2</option>
            </select>

It happens that this is a list to send the data in a form of paypal and I have the following

$pp = array( 
    'cmd' => '_xclick', 
    'cert_id' => $cert_id, 
    'business' => '[email protected]',                 
    'receiver_email' => '[email protected]',                 
    'custom' => '1',          
    'item_name' => **>>Justo acá quiero que me aparezca el "value" osea en la option 1 que me salga 100 <<** ,                  
     'currency_code' => 'USD',                            
    'amount' => '1',                                     
   .....................
);

Well I was trying with javascript with an onclick in the form and adding a post in item_name example $ _POST ['test'],

while the javascript code I tried was this

function mostrarValue(){

    var selObj = document.getElementById('test');
       var selIndex = selObj.options[selObj.selectedIndex].value;
    }

I hope and someone can guide me in this I would be very grateful, greetings and thanks in advance.

This is the code itself I'm using.

   <script language="javascript"> 
function mostrarValue(){

    var selObj = document.getElementById('test');
       var selIndex = selObj.options[selObj.selectedIndex].value;

    }
</script> 
<p>Seleccione uno de los articulos</p>
        <label class="youplay-select">
                <select  id="test" name="test">
                <option value="" selected>Seleccionar</option>
                <option value="1000">Articulo1</option>
                <option value="1001">Articulo2</option>
                <option value="1002">Articulo3</option>

            </select>
        </label>


<?php
$cert_id = 'CERTIFICADOPAYPAL'; 
include("Class.PayPalEWP.php"); 
$paypal = new PayPalEWP(); 
$paypal->setTempFileDirectory("tmp");
$paypal->setCertificate("my-pubcert.pem", "my-prvkey.pem"); 
$paypal->setPayPalCertificate("paypal_cert_pem.txt"); 
$paypal->setCertificateID($cert_id);  

$pp = array( 
        'cmd' => '_xclick', 
        'cert_id' => $cert_id, 
        'business' => '[email protected]',                 
        'receiver_email' => '[email protected]',                 
        'custom' => '1',         
        'item_name' $_POST['test'],                  
         'currency_code' => 'USD',                             
        'amount' => '1',                                   
        'lc' => 'ES',                                         
        'no_note' => '1',                                     
        'no_shipping' => '1',                                  
        //'return' => '',                     
        'cancel_return' => '',             
        //'notify_url' => '',               
        'cbt' => 'Volver al sitio web', 


    );

?> 


  <form action="https://www.sandbox.paypal.com/cgi-bin/webscr"  id="pay_pal_form" method="POST" name= "form1" onclick="mostrarValue();">

  <input type="hidden" name="cmd" value="_s-xclick"><input type="hidden" name="cmd" value="_s-xclick"> 
  <input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----<?php echo $paypal->encryptButton($pp); ?>-----END PKCS7-----"/> 

  <input type="image" src="https://www.paypal.com/es_XC/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal, la forma más segura y rápida de pagar en línea."> 
  <img alt="" border="0" src="https://www.paypal.com/es_XC/i/scr/pixel.gif" width="1" height="1"></p></div>
</form>

I want this to send me the value of the "item_name" so in the ipn insert it in a db, but in the ways I've tried it saves what is "Price1" and not "100" and with the method that I highlight in my code when sending the form asks me to enter a new name, it takes as if it did not have any item_name

    
asked by José 18.11.2017 в 09:58
source

3 answers

1

I could use Jquery, in this way to obtain the value of the selection would be very easy, an example would be the following:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select  id="test">
<option value="100" selected>Precio1</option>
<option value="200" selected>Precio2</option>
</select>

<button onclick="mostrarValue()">
Mostrar valor
</button>

<script>
function mostrarValue(){
alert("Valor de #test: "+$("#test").val())
}
</script>

greetings

    
answered by 18.11.2017 в 10:18
1

Example to do it with $_POST , notice that I have changed the attribute id that you have in the form by the attribute name . Since what you send in the form is the attribute name and the attribute value .

/* formulario.html */
<form method="post" action="proceso.php">
  <select name="test">
    <option value="A">Artículo A</option>
    <option value="B">Artículo B</option>
   </select>
  <input type="submit" value="Submit del form"/>
</form>
/*En el fichero proceso.php*/
    if(isset($_POST['test'])) 
        echo $_POST['test']; // Sacas el valor
    
answered by 18.11.2017 в 22:38
1

If I understood correctly, you just want to get the value of the selected select to be able to send a request?

That you achieve by obtaining the value value of the element. For example:

let obtenerValor = function(e){
  let select = document.querySelector('#auto');
  console.log(select.value)
}
<select name="auto" id="auto">
   <option value="1">Volvo</option>
  <option value="2">Saab</option>
  <option value="3">Mercedes</option>
  <option value="4">Audi</option>
</select>

<button onclick="obtenerValor()">Obtener Valor</button>

I hope it was useful, greetings.

    
answered by 20.11.2017 в 15:41