Andrei
Andrei

Reputation: 31

Setting the value of a input from another input

So, I have this button where you type the title and press on "create", here is a photo: enter image description here

Here is the code:

<div class="input-group margin">
                <div class="input-group-btn">
                  <button type="button" name="artButton" data-toggle="modal" data-target="#createArticle" class="btn btn-danger">Create</button>
                </div>
                <!-- /btn-group -->
                <input type="text" class="form-control" id="artTitle" name="artTitle" placeholder="Article title">
              </div>

    </div>

What I tried was to surround it with two form tags as POST method and then use $_POST['artTitle'] to echo what the user inserted in the other input but it didn't work.

So basically, when you press on create, a modal shows up asking for information regarding the topic, and I want the title to autofill if you type it in that field before pressing create..., and it doesn't work...

Here is how I tried to add the value to the other input

<?php if(isset($_GET) && array_key_exists('artButton',$_GET)): ?>
                <div class="form-group">
                  <label for="createArticleTitle">Title:</label>
                  <input type="input" class="form-control" value="<?php echo $_GET['artTitle']?>" id="createArticleTitle" name="createArticleTitle" maxlength="80" placeholder="Insert Article Title...">
                </div>
                <?php endif; ?>

Upvotes: 0

Views: 87

Answers (1)

JYoThI
JYoThI

Reputation: 12085

I think you need after click create article field autofill in modal too . just use onlick event like this

$('#btnclick').click(function()
{
  // alert($('#artTitle').val());
   $('#article1').val($('#artTitle').val());

});
<form>
<div class="input-group margin">
                <div class="input-group-btn">
                  <button type="button" name="artButton" data-toggle="modal" data-target="#createArticle" id="btnclick"class="btn btn-danger">Create</button>
                </div>
                <!-- /btn-group -->
                <input type="text" class="form-control" id="artTitle" name="artTitle" placeholder="Article title">
              </div>

    </div>
    
 </form>   
 
 <head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  
  <!-- Trigger the modal with a button -->
<!--  <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> -->

  <!-- Modal -->
  <div class="modal fade" id="createArticle" role="dialog">
    <div class="modal-dialog">
    
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
          <p>Input atricle name<input type="article" id="article1" /></p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>

</body>
</html>

Upvotes: 1

Related Questions