Pass PHP value to the SimpleModal Form via GET

0

I'm using Eric Martin's SimpleModal Contact Form , but I'm having a hard time passing a variable to the form via GET.

I wanted the index.php , I could send the variable $palestra_titulo , and that it was used in the Form in the "Subject" field, already appearing filled and being used to send by email. >

In index.php, the GET method would be passed through an image. It would be something like <a href='data/contact.php?article_id=<?php $id ?>' class="contact"><img src="x.gif"/></a> .

In contact.js, I have an ajax function:

    $.ajax({
      url: 'data/contact.php',
      data: $('#contact-container form').serialize() + '&action=send&',
      type: 'post',
      cache: false,
      dataType: 'html',
      success: function (data) {
          $('#contact-container .contact-loading').fadeOut(200, function () {
            $('#contact-container .contact-title').html('Obrigado!');
            msg.html(data).fadeIn(200);
          });
       },
       error: contact.error
     });

And in contact.php, the snippet that displays the field is:

    $action = isset($_POST["action"]) ? $_POST["action"] : "";
    if (empty($action)) {
  // Send back the contact form HTML
  $output = "<div style='display:none'>
  <div class='contact-top'></div>
  <div class='contact-content'>
    <h1 class='contact-title'>Inscreva-se Aqui</h1>
    <div class='contact-loading' style='display:none'></div>
    <div class='contact-message' style='display:none'></div>
    <form action='#' style='display:none'>
       <label for='contact-name'>*Nome:</label>
       <input type='text' id='contact-name' class='contact-input' name='name' tabindex='1001' />
       <label for='contact-email'>*E-mail:</label>
       <input type='text' id='contact-email' class='contact-input' name='email' tabindex='1002' />";

if ($extra["form_subject"]) {
    $output .= "
    <label for='contact-subject'>Palestra:</label>
    <input type='text' id='contact-subject' class='contact-input' name='subject' value='' tabindex='1003' />";
}

The value that should display the value of the $ lecture_title is in the last line.

What changes in contact.js and contact.php should I do for this?

    
asked by anonymous 15.05.2014 / 17:31

1 answer

0

Given the lack of important information such as your current HTML as well as a slightly more detailed description of what you're trying to do, I'll assume the two best scenarios I can build with what I have at my disposal.

Scenario # 1

The term paletras_titulo is already present in the URL of the page that will open the modal. In this case you simply follow the commented suggestion of friend Anddrey and echo the value n = within the value attribute of the desired input .

However, slightly different from what he suggested, instead of echoing any variable, you will pull such information from the superglobal $_GET :

<input value='<?php echo ( isset( $_GET['palestra_titulo'] ) ? $_GET['palestra_titulo'] : '' ) ?>' />

Scenario # 2

The term paletras_titulo is not present in the URL of the page that will open the modal.

In this case you have at least two options:

1) Fill in JavaScript with the value attribute of the HTML element in question by merging JS with some PHP variable. Something like this:

<?php

echo '<script type="text/javascript">

    $( document ).ready( function() {

        $( "#title" ).val( "{$paletra_titulo}" );
    });

</script>';

?>

<!-- Mais HTML -->

<form id="modal">
    <input id="title" name="title" value="" />
</form>

Personally I find this extremely damaging to maintainability of the Application.

2) If you use the onOpen () or onShow () callbacks and request the title of the page by AJAX. Something like this:

$("#element-id").modal(

    {
        onOpen: function() {

            $.ajax({
                url: "sua_url.php",
                data: { id: 123 },
                success: function( response ) {

                    $( '#title' ).val( response );
                }
            });
        }
    }
);

Note that 123 should not be used. This should be replaced by the actual value of the ID of the lecture that is most likely present in the URL, also in the form of querystring, since this is the most basic way of passing data in a CRUD.

Also note that depending on what you have as output from the URL named here your_url.php , the response within jQuery.val () can change.

If anyone has any suggestion or syntax correction, please feel free.

    
15.05.2014 / 21:28