Pass PHP value to Simplemodal Form via GET

Asked

Viewed 1,756 times

0

I’m using the Simplemodal Contact Form by Eric Martin, but I’m having a hard time passing a variable to the form via get.

I wanted to, in the index.php, I could send the variable $palestra_titulo, and that this was used in the form in the "Subject" field, already appearing filled and being used to send by e-mail.

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 $palestra_title variable is in the last line.

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

  • It is not good for you to print the variable in the attribute value of input? Something like: <input value='<?php echo $palestra_titulo ?>'>...

  • 4

    Welcome to the Sopt. Just to help you get used to our philosophy, which is different from a forum, take a look at pt.stackoverflow.com/help/behavior, especially 3rd. item. If not read yet, it would be good to take a look at [about], you win a medal. You can use [Edit] to leave your question in the way of a straight and clean question. Gradually you get used to it.

1 answer

0


Given the lack of important information, such as your current HTML, as well as a slightly more detailed description of what exactly you are trying to do, I will take on the two best scenarios I can build with what I have at hand.

Scenario #1

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

However, slightly different from what he suggested, rather than 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 that case you have at least two options:

1) Fill, with Javascript, the value attribute of the HTML element in question by merging the 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 the maintainability of the Application.

2) Using the callbacks onOpen() or onShow() plugin and request the title of the palaestra 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 lecture ID that is most likely present in the URL, also in the form of querystring, since this is the most basic way of transiting data in a CRUD.

Also note that depending on what you have as output of the URL named here sua_url.php, the sponse inside jQuery.val() can change.

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

  • Thank you very much for the answers. As suggested, I edited the question, adding more information.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.