Update a url link based on user text input and save change in WordPress

I’ve created a WordPress page that in summary, allows someone to start a hangout session. Once the hangout session is started, the user can copy the hangout URL and paste it into an input box, then click submit. The intent is that the updated link on the page stays around for the next user that opens the page to click and follow to the same hangout.

I’m using the following JS function to update the html portion listed below.

Read More
 <script type="text/javascript">
function updatelink(){
    var userInput = document.getElementById('userInput').value;
    var lnk = document.getElementById('lnk');
    lnk.href = userInput;
    lnk.innerHTML = lnk.href;
}
</script>

Here’s the html that calls the function and get’s updated.

Connect to Hangout Here : <a href="" id=lnk>nothing here yet</a> <br>
<input type='text' id='userInput' value='Enter Hangout Link Here' />
<input type='button' onclick='updatelink()' value='Change Link'/>

This works to update the link on the page to the user’s input. The problem i’m having is that it doesn’t save when you refresh the page. It resets when I need it to stay until it gets updated again.

Any help would be appreciated. I’m a beginner, so the more descriptive the better.

Thanks.

Related posts

Leave a Reply

2 comments

  1. <input type='text' id='userInput' name='urlhang' value='Enter Hangout Link Here' />
    
    
    SESSION['urlhang'] = $_POST['urlhang'];
    

    You can create the session of that value & when you want to update that value reset the session.

    May this logic work for your issue. so you can get that value in SESSION[‘urlhang’] as it is till you will update it.

  2. I’ve found that it may be best for me to use MySQL to update and store the link in a table.

    So far I’ve achieved pulling the lnk from my database using the following code:

    extract(shortcode_atts(array('arg' => 'default'), $atts));
       // Connects to your Database 
       mysql_connect("localhost", "user", "password") or die(mysql_error()); 
       mysql_select_db("my_db") or die(mysql_error()); 
       $data = mysql_query("SELECT * FROM lnk") 
       or die(mysql_error());
    
       while($info = mysql_fetch_array( $data )) 
       {  
       return "".$info['lnk']."";
       }  
    

    The first line is creating a shortcode for wordpress. My html that accesses this php script is as follows:

    <button onclick="openWin()" name="hangout1">Join/Create Room</button>
    <script type="text/javascript">// <![CDATA[
    function openWin() { window.open(lnk,"_blank","toolbar=0, scrollbars=0, resizable=yes, top=-100, left=0, width=800, height=800"); }
    // ]]></script>
    
    <a id="lnk" href=[connectserver]></a>
    

    [connectserver] is the php shortcode reference.

    The code I have to update the link in the database is as follows:

    extract(shortcode_atts(array('link' => 'default'), $atts));
    
     // Connects to your Database 
     $con=mysqli_connect("localhost","username","password","my_db");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }
    mysqli_query($con,"UPDATE lnk SET lnk='"$atts"'"); 
    
    
    
    mysqli_close($con);
    

    The following script is used to submit the new link(user input) to the database through the php shortcode above. (when running the php manually, I can change the lnk='”$atts”‘”); portion to my desired variable ex… lnk=’www.yourwebsite.com'”); and it works properly.) (see the rest of the non-working code below.)

    <input id="userInput" type="text" value="Enter Hangout Link Here" />
    <input onclick="[updatelnk link=userinput]" type="button" value="Change Link" />
    

    I can’t figure out how to pass the new link via user input to the shortcode. Does anyone have an answer for this?

    Thanks for the answer Dipak, but i’m a novice and have no experience with javascript. I looked up starting a session but I think the database option may be a better avenue. If I’m wrong please let me know.

    Ken